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");
}}
您想循环多少次? 10 循环时间是:10 猜测的最大数字是多少? 10 随机数是:0 猜数是:2 猜数是:4 猜数是:5 猜数是:6 猜数是:0
---- jGRASP wedge2:进程的退出代码为0。 ---- jGRASP:操作完成。 我希望计算机随机化数字,直到随机和猜测相同,我不想继续输入数字。 代码永远不会显示“猜测和随机是相同的”行,而只是保持随机猜测并保持随机相同。我该如何改变?
答案 0 :(得分:2)
这应该按照我的想法做到:
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);
}
}
答案 1 :(得分:0)
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);