Results 1 to 8 of 8
  1. #1
    sarchie109 is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Exclamation Need to end program by typing quit and not -1!!! I have pasted my code for assistance

    import java.util.*;
    import java.util.Random;

    public class SimpleMath
    {
    public static void main(String [] args)
    {
    int number1;
    int number2;
    int sum;
    int useranswer=0;


    Scanner keyboard = new Scanner(System.in);

    Random randomNumbers = new Random();


    while (useranswer>-1)
    {
    number1=randomNumbers.nextInt(10);
    number2=randomNumbers.nextInt(10);

    System.out.println("What is the answer to the problem?");
    System.out.println(number1+ " + " + number2 +" = ? ");

    sum=number1+number2;
    useranswer=keyboard.nextInt();


    if (useranswer==sum)
    System.out.println("True!");
    if (useranswer!=sum)
    System.out.println("False. The correct answer is "+sum);





    }
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    So use next (or nextLine) and Integer.valueOf rather than nextInt.

  3. #3
    sarchie109 is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    So where do I put the next (nextLine) statement? I'm still confused. And dont i have to have a string variable for quit in there?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Like I said "instead of nextInt". And, of course, that returns a Sring, so the value will first be put into a String a variable and that String then parsed using Integer.valueOf into an int.

    Did you at least try doing anything with the advice above?

  5. #5
    sarchie109 is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default Assistance

    I used that and it kept saying integer.valueOf wasnt a method or something like that. I'm not sure how to type that in the IDE and the location of it....

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

  7. #7
    sarchie109 is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Smile Thanks

    I took the wrong approach to the problem but thanks for your help anyway. I hope I can gain future assistance from you.

  8. #8
    Join Date
    Nov 2009
    Posts
    22
    Rep Power
    0

    Default

    post good, thank you
    :D I'm from vietnam - I hope you help me :D

Similar Threads

  1. Replies: 1
    Last Post: 08-20-2009, 04:28 PM
  2. How to exit the program by typing "exit"?
    By Laythe in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 08:32 PM
  3. Replies: 3
    Last Post: 04-20-2009, 09:53 PM
  4. Quit Button, Load new Frame, Timer Code
    By IHateNetbeans in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 03:58 PM
  5. In need of some assistance
    By Boer84 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 04:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •