Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Trouble with comparing variables.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble with comparing variables.

    I have a math program that generates random numbers but i cannot figure out why the answer always says not correct. My logic seems sound but i might be missing something.
            Variable l = new Variable();
     
            int a = l.roll(1, 9);
            int b = l.roll(1, 9);
            int check = a + b;
     
     
            int s = Integer.parseInt(Answer.getText());
     
     
     
     
            if (s == check) {
                main.setText("");
                main.append("correct\n");
                main.append("Next Question:\n What is " + a + " + " + b);
            }else {
                main.setText("");
                main.append("Not Correct\n");
                main.append("Try again:\n What is " + a + " + " + b);
            }
            System.out.println(check);
    variable holds my random code.
    As i said before i always get the else statment even though the answer is correct. it should be going to the if statment if correct and the else statment if not correct.
    What am i doing wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble with comparing variables.

    Print out the values of s and check just before the if statement that tests them so you can see what the computer is seeing and understand why it is doing what it does.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble with comparing variables.

    Quote Originally Posted by miller4103 View Post
    ... i always get the else statment ...
    Then either s or check have a value you do not expect. Use a println to see the values of both variables just before your if statement and see which one (or if both) have the value you expect.

  4. #4
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with comparing variables.

    I figured it out but do not know how to fix it. What is happening is it wants the value of the next numbers rolled not the ones that are currently on the screen. so if the problem is 7+1 and the second problem is 9+1 i have to enter 10 for the 7+1 for it to be correct. How do i fix this? I cant figure it out.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble with comparing variables.

    Change the logic so it is comparing what's there now.
    Don't show data for the next problem until the current problem has been handled.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member didingnuriska's Avatar
    Join Date
    Apr 2013
    Posts
    21
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with comparing variables.

    I think this code work well..
    If s is the guessed number by user for (a+b)?
    so you need to guess the right number for "correct printed"...

    this is my simply test code, and work fine..

    public static void main(String args[]) {
            int a=new Random().nextInt(5);
            int b=new Random().nextInt(5);
     
            int c=a+b;
     
            int ans=5;
            System.out.println(c);
            if(c==ans){
                System.out.println("your guessed = "+ans+" is correct" +" a="+a+" b="+b);
            }else{
                                System.out.println("your guessed = "+ans+" is not correct"+" a="+a+" b="+b);
     
            }
     
        }

Similar Threads

  1. Having trouble loading variables from a file
    By Gravity Games in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 20th, 2012, 10:35 PM
  2. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  3. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 08:34 PM
  4. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  5. [SOLVED] Trouble accessing variables from other classes
    By Elementality in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 5th, 2011, 11:50 AM