| | |
Java math program
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2006
Posts: 2
Reputation:
Solved Threads: 0
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
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
- What is wrong with my java program (Java)
- Java BankAccount program (Java)
- New to Java, can't get program to compile (Java)
- Very New to java, skeleton of program given (Java)
- cheeky java program (Java)
- java client server quiz. interesting!!! (Java)
Other Threads in the Java Forum
- Previous Thread: calculator
- Next Thread: Data Models and Pattern Matching
| Thread Tools | Search this Thread |
add android api applet application applications array arrays automation bank binary bluetooth chat class classes clear client code collections component converter database development dice digit eclipse equation error event exception formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jni jpanel julia linux list loop looping main map method methods mobile myregfun netbeans newbie nonstatic openjavafx pearl php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting spamblocker sql sqlserver state storm string superclass swing text-file thread threads time tree windows






