Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8484473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:23:25+00:00 2026-06-10T20:23:25+00:00

The program draws two digits, and the sign. IF statement checks to see if

  • 0

The program draws two digits, and the sign. IF statement checks to see if it has been drawn + / -. If it draws + add, if – is subtraction.

Draw works.

Then the user is given the result of the task. And here is the problem.

If you give a result which is in the “result”. Function if something does. If you entered an incorrect answer is displayed Toast: Try Again.

The problem is that sometimes as to give a good result is is displayed Try Again.

How to eliminate this problem? Might different check?

Code:

private String sign;
    private int numberOne, numberTwo, result = 0;
    private int charsEntered = 0;
    private EditText et;
    private Button ok;
    String[] CHAR = { "+", "-" };
    Random intGen = new Random();
    CaptchaInterface.OnCorrectListener mCorrectListener;

    public void setOnCorrectListener(CaptchaInterface.OnCorrectListener listener) {
        mCorrectListener = listener;
    }

    public EasyMathCaptcha(Context context) {
        super(context);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    }

    public static int randomOne() {
        Random generator = new Random();
        int x = generator.nextInt(10);
        return x;
    }

    public static int randomTwo() {
        Random generator = new Random();
        int x = generator.nextInt(10);
        return x;
    }

    public void onCreate(Bundle icicle) {
        setContentView(R.layout.all_math_captcha);

        sign = (CHAR[Math.abs(intGen.nextInt() % 2)]);
        numberOne = randomOne();
        numberTwo = randomTwo();

        TextView display = (TextView) findViewById(R.id.tvRandomTask);
        display.setText(numberOne + " " + sign + " " + numberTwo);

        if ((CHAR[Math.abs(intGen.nextInt() % 2)]).equals("+")) {
            result = (numberOne + numberTwo);
        } else if ((CHAR[Math.abs(intGen.nextInt() % 2)]).equals("-")) {
            result = (numberOne - numberTwo);
        }

        et = (EditText) findViewById(R.id.etTask);
        ok = (Button) findViewById(R.id.btAgree);
        ok.setOnClickListener(this);

    }

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        try {
            charsEntered = Integer.parseInt(et.getText().toString());
        } catch (NumberFormatException nfe) {
            Toast.makeText(et.getContext(), "That's not a number!",
                    Toast.LENGTH_SHORT).show();
        }

        if (charsEntered == result) {
            if (mCorrectListener != null)
                mCorrectListener.onCorrect();
            dismiss();

        } else if (charsEntered != result) {
            Toast.makeText(et.getContext(), "Try again!", Toast.LENGTH_SHORT)
                    .show();
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:23:26+00:00Added an answer on June 10, 2026 at 8:23 pm

    The error is in the following code:

        if ((CHAR[Math.abs(intGen.nextInt() % 2)]).equals("+")) {
            result = (numberOne + numberTwo);
        } else if ((CHAR[Math.abs(intGen.nextInt() % 2)]).equals("-")) {
            result = (numberOne - numberTwo);
        }
    

    You are using the random number generator which can give you results different than what it gave the first time.

    Change it to:

        if (sign.equals("+")) {
            result = (numberOne + numberTwo);
        } else if (sign.equals("-")) {
            result = (numberOne - numberTwo);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program has a window where to draw and works on a model (a
I have my program that can draw rectangles. I have two problems I can't
I have a program where I want to draw a line between two points.
I am trying to add some shaders to my old OpenGL program that draws
I've been working on a program to draw cards. I have 54 cards and
In my program, I draw some quads. I want to add the functionality for
I've been thinking about a program logic, but I cannot draw a conclusion to
I have written a program with two components that use mouseListeners: ResizeRectangle draws a
My program draws heavily on a winform. During the drawing, the winform is not
I have a program that draws some vector graphics using System.Drawing and the Graphics

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.