Reputation: 29
import java.util.*;
/*
10,100,1000 intervals of 10
App generates a number:unkown
person enters a number
Miguel Castaneda
*/
class GenerateNumber{
public static void main(String args[])
throws java.io.IOException {
int num;
/*
loopTimes = System.in.read(); //casted
System.out.println("Loop Times is: " +loopTimes)
*/ //one byte at a time gets read
System.out.println("How many times do you want to loop?");
Scanner sc = new Scanner(System.in);
int loopTimes = sc.nextInt(); //new scanner
System.out.println("Loop Times is: " +loopTimes);
System.out.println("What would be the max number to guess?");
//genearte random number named :randomNumber
int maxNumber = sc.nextInt();
Random generator = new Random();
int i = generator.nextInt(maxNumber);
int guess = generator.nextInt(maxNumber);
System.out.println("Random number is: " + i);
System.out.println("Guess number is: " + guess);
while(i!=guess){
if(guess==i)
System.out.println("Guess and Random are same");
guess = generator.nextInt(maxNumber);//guess until random and guess are same
System.out.println("Guess number is: " + guess); //print out everytime guess and random arent the same
counter++;
}
//loopTimes = System.in.read(); //casted
//if(num==rand)System.out.println("Right");
//else System.out.println("Wrong");
}}
How many times do you want to loop? 10 Loop Times is: 10 What would be the max number to guess? 10 Random number is: 0 Guess number is: 2 Guess number is: 4 Guess number is: 5 Guess number is: 6 Guess number is: 0
----jGRASP wedge2: exit code for process is 0. ----jGRASP: operation complete. I want the computer to keep randomizing the number until random and guess are same I don't want to keep entering numbers. The code never shows that "Guess and random are same" line instead it just keeps randomizing the guess and leaves random same. How do I change this?
Upvotes: 0
Views: 311
Reputation: 6096
int i;
while(i< loopCount) {
i++;
System.out.println("Guess number " + i + ":");
int userGuess = sc.nextInt();
if (randomNumber == userGuess) {
System.out.print("Success Random number was " + randomNumber);
sc.close();
break;
} else {
System.out.println("Keep Guessing!");
}
}
sc.close();
if(i==loopTimes)
System.out.println("Failed The number was " + randomNumber);
Upvotes: 0
Reputation:
This should do what you want it to I think:
import java.util.*;
/*
10,100,1000 intervals of 10
App generates a number:unkown
person enters a number
*/
class GenerateNumber {
public static void main(String args[])
throws java.io.IOException {
int randomNumber;
int loopTimes;
/*
loopTimes = System.in.read(); //casted
System.out.println("Loop Times is: " +loopTimes)
*/ //one byte at a time gets read
System.out.println("How many times do you want to loop?");
Scanner sc = new Scanner(System.in);
loopTimes = sc.nextInt(); //new scanner
System.out.println("Loop Times is: " +loopTimes);
System.out.println("What would be the max number to guess?");
int maxNumber = sc.nextInt();
Random generator = new Random();
randomNumber = generator.nextInt(maxNumber);
for (int i=1;i<=loopTimes;i++) {
System.out.println("Guess number " + i + ":");
int guess = sc.nextInt();
if (randomNumber == guess) {
System.out.print("YOU WIN! Random number was " + randomNumber);
sc.close();
System.exit(0);
} else {
System.out.println("Keep Guessing!");
}
}
sc.close();
System.out.println("SORRY! YOU LOSE! The number was " + randomNumber);
}
}
Upvotes: 2