User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 392,071 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,248 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1952 | Replies: 2 | Solved
Reply
Join Date: Jul 2006
Posts: 2
Reputation: Skotie44 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Skotie44 Skotie44 is offline Offline
Newbie Poster

Java math program

  #1  
Jul 24th, 2006
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,668
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 17
Solved Threads: 298
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Java math program

  #2  
Jul 24th, 2006
Have a while loop:
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Jul 2006
Posts: 2
Reputation: Skotie44 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Skotie44 Skotie44 is offline Offline
Newbie Poster

Re: Java math program

  #3  
Jul 24th, 2006
I figured it out. Thanks
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 12:00 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC