943,972 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 8785
  • Java RSS
Jul 24th, 2006
0

Java math program

Expand Post »
Hello All

I am writing this program which:
- generates two random numbers
-ask user to multiply them
-checked users answer to see if its correct.
- if not correct, asked the user to guess again

I have everything working except the loop that is needed to have the user continue to guess if inputing the wrong answer.

As it stands right now, if the user answers correct, get a message saying "you answered correct.

If user is wrong, gets message saying "Guess Again". I need to figure out how to have the program loop back around to have user input a new answer until user gets it right


import java.io.*;
import java.util.Random;
public class math
{
//-----------------------------------------------------------------
// Generates random numbers
//-----------------------------------------------------------------
public static void main (String[] args)throws IOException
{
DataInputStream stdin = new DataInputStream (System.in);
Random generator = new Random();
int num1;
int num2;
int guess;
int answer;
String string1;

num1 = generator.nextInt(13); //Gets random num1

num2 = generator.nextInt(13); //Gets Random num2

System.out.println ("what is " + num1 + "*" + num2);


string1 = stdin.readLine();
guess = Integer.parseInt(string1);
answer = (num1 * num2);



if (guess == answer)
{ //Begin if bracket
System.out.println("Very good, the correct answer is " + answer);
} //End if Bracket
else
{ //Begin Else Brakcets
System.out.println("Guess again");
}
//End Else brackets
}

} //Close MAIN bracket


} //Close CLASS MATH bracket
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Skotie44 is offline Offline
2 posts
since Jul 2006
Jul 24th, 2006
0

Re: Java math program

Have a while loop:
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 24th, 2006
0

Re: Java math program

I figured it out. Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Skotie44 is offline Offline
2 posts
since Jul 2006
Oct 28th, 2010
0
Re: Java math program
Write a java program to find the result of the following
equations for the values given.
a. sin^(theta) + cos^2(theta) where theta=90 degrees
b. (x+y)^2/(x-y)^2 where x=4.0, y=2.0
c. logx + x^3 where x=10
d. e^x -2x + 3y where x =1, y=2.5
e. (x+y)^3 where x=1, y=1
f. min(x,y) and max(x,y) where x=-2.5, y=-2.5123
Note: the name of the class must be H5
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hassin is offline Offline
10 posts
since Oct 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: .txt file to object
Next Thread in Java Forum Timeline: Macro in Java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC