Computing.Net > Forums > Programming > Need help with loop......

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Need help with loop......

Reply to Message Icon

Name: Pearljam04
Date: March 20, 2003 at 10:36:17 Pacific
OS: XP
CPU/Ram: XP 2000+/ 512Mb DDR
Comment:

Hi,

im needing to make a java program that works with random numbers. Im trying to generate random numbers then have the program so a user manually guesses each number.

I have came up with this piece of code to generate numbers....

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

public static void main(String[] args){
Random rand = new Random();
int randNum1=rand.nextInt(9);
int randNum2=rand.nextInt(9);
int randNum3=rand.nextInt(9);
}
}

This works fine for generating the numbers but im gettin into difficulty trying to do the guessing bit....

I need a loop so that the 1st number has to be correct then the 2nd number then the 3rd, ie in order. It needs to tell the user if they are correct or not then if they are, move them onto the next number. I also need a count for how many guesses they make in total.

If anyone has any ideas on how i could do this i would be extrememly greatful.

Thanks

Best Wishes.



Sponsored Link
Ads by Google

Response Number 1
Name: gpp
Date: March 20, 2003 at 11:12:42 Pacific
Reply:

A quick example.. I'll let you fill in the rest.

import java.util.Random;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;

public class NumGuess {

public static void main(String[] args) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Random rand = new Random();
int randNum1=rand.nextInt(9);
int randNum2=rand.nextInt(9);
int randNum3=rand.nextInt(9);

boolean done=false;
boolean firstCorrect=false;
boolean secondCorrect=false;
boolean thirdCorrect=false;

int guessCount=0;

do{
System.out.println("Numbers(just for reference.. remove later on): "+randNum1+" "+randNum2+" "+randNum3);
System.out.println("Enter you guess: ");
String guessString="";
int guessInt=0;
boolean badInput=false;
try {
guessString = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error, quitting!");
System.exit(1);
}
try{
guessInt=Integer.parseInt(guessString);
} catch(NumberFormatException nfe){
badInput=true;
System.out.println("Numbers Only!");
}
if(!badInput){
if(!firstCorrect){ //match it!
if(guessInt == randNum1){
firstCorrect=true;
System.out.println("Got the first one!");
done=true;
}
}

//Try to match the rest.....
}

} while(!done);
} catch (Exception e){
System.out.println("Could not recover from error!");
}
}

}


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Need help with loop......

Need help with WinXP batch script www.computing.net/answers/programming/need-help-with-winxp-batch-script/13165.html

Need help with an SQL join query www.computing.net/answers/programming/need-help-with-an-sql-join-query/13358.html

Need help with algorithm!! www.computing.net/answers/programming/need-help-with-algorithm/594.html