我有一个程序编码如下,我试过但它总是只读取第一个数字......
“仅输入第一个数字的程序,用户程序输入的数字的其余部分忽略并始终读取第一个数字?” “另外,请仅针对特定数字解决math.random”。
public static void main(String[] args) {
Scanner reading = new Scanner(System.in);
int randomNumber = (int) (Math.random() * 50-1);
int input;
int guessNumber;
System.out.println(" Welcome UCT beginers Game we will tes your IQ ");
for (int i=1; i<3; i++)
{
System.out.println(" Please select your level Game ");
System.out.println("1. Three chances to play.");
System.out.println("2. Five chances to play");
System.out.println("3. Ten chances to play");
System.out.println("4. Exit the Game");
input = reading.nextInt();
switch (input)
{
case 1:
System.out.println(" Welcome You have chosen level ONE. This is normally can pass only with higher IQ");
System.out.println("Please Guess my number");
guessNumber = reading.nextInt();
if (randomNumber == guessNumber )
{
System.out.println(" Congratulation; You have earned 3 coins");
break;
}
else if (randomNumber > guessNumber )
{for (int a = 1; a < 3; a++)
{
System.out.println(" Sorry Low number");
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *** The number was "+randomNumber);
}
else if (randomNumber < guessNumber)
{for (int a = 1; a < 3; a++)
{
System.out.println(" Sorry High number" );
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *****The number was "+ randomNumber);
}
break;
case 2:
System.out.println(" Welcome You have chosen level TWO. This is normally can pass middale IQ");
System.out.println("Please Guess my number");
guessNumber = reading.nextInt();
if (randomNumber == guessNumber )
{
System.out.println(" Congratulation; You have earned 3 coins");
break;
}
else if (randomNumber > guessNumber )
{for (int a = 1; a < 5; a++)
{
System.out.println(" Sorry Low number");
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *** The number was "+randomNumber);
}
else if (randomNumber < guessNumber)
{for (int a = 1; a < 5; a++)
{
System.out.println(" Sorry High number");
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *** The number was"+randomNumber);
}
break;
case 3:
System.out.println(" Welcome You have chosen level THREE. This passes normally every person");
System.out.println("Please Guess my number");
guessNumber = reading.nextInt();
if (randomNumber == guessNumber )
{
System.out.println(" Congratulation; You have earned 3 coins");
break;
}
else if (randomNumber > guessNumber )
{
for (int a = 1; a < 10; a++)
{
System.out.println(" Sorry Low number");
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *** The number was "+randomNumber);
}
else if (randomNumber < guessNumber)
{
for (int a = 1; a < 10; a++)
{
System.out.println(" Sorry High number");
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *** The number was "+randomNumber);
}
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid entary");
break;
}
}
}
答案 0 :(得分:0)
public static void main(String args[]) {
int input;
int guessNumber;
Scanner reading = new Scanner(System.in);
int randomNumber = (int) (Math.random() * 50 - 1);
System.out.println(" Welcome UCT beginers Game we will tes your IQ ");
System.out.println(" Please select your level Game ");
System.out.println("1. Three chances to play.");
System.out.println("2. Five chances to play");
System.out.println("3. Ten chances to play");
System.out.println("4. Exit the Game");
while (reading.hasNext()){
input = reading.nextInt();
switch (input) {
case 1:
System.out.println(" Welcome You have chosen level ONE. This is normally can pass only with higher IQ");
System.out.println("Please Guess my number");
guessNumber = reading.nextInt();
if (randomNumber == guessNumber) {
System.out.println(" Congratulation; You have earned 3 coins");
break;
} else if (randomNumber > guessNumber) {
for (int a = 1; a < 3; a++)
{
System.out.println(" Sorry Low number");
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *** The number was " + randomNumber);
} else if (randomNumber < guessNumber) {
for (int a = 1; a < 3; a++) {
System.out.println(" Sorry High number");
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *****The number was " + randomNumber);
}
break;
case 2:
System.out.println(" Welcome You have chosen level TWO. This is normally can pass middale IQ");
System.out.println("Please Guess my number");
guessNumber = reading.nextInt();
if (randomNumber == guessNumber) {
System.out.println(" Congratulation; You have earned 3 coins");
break;
} else if (randomNumber > guessNumber) {
for (int a = 1; a < 5; a++) {
System.out.println(" Sorry Low number");
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *** The number was " + randomNumber);
} else if (randomNumber < guessNumber) {
for (int a = 1; a < 5; a++) {
System.out.println(" Sorry High number");
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *** The number was" + randomNumber);
}
break;
case 3:
System.out.println(" Welcome You have chosen level THREE. This passes normally every person");
System.out.println("Please Guess my number");
guessNumber = reading.nextInt();
if (randomNumber == guessNumber) {
System.out.println(" Congratulation; You have earned 3 coins");
break;
} else if (randomNumber > guessNumber) {
for (int a = 1; a < 10; a++) {
System.out.println(" Sorry Low number");
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *** The number was " + randomNumber);
} else if (randomNumber < guessNumber) {
for (int a = 1; a < 10; a++) {
System.out.println(" Sorry High number");
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *** The number was " + randomNumber);
}
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid entary");
break;
}
}
}
答案 1 :(得分:0)
当我运行代码时,它会读取所有用户输入。但是,它不会按预期处理它。我认为这个想法是用户可以猜几次 - 每次告诉用户猜测的数字是太高还是太低,他/她可以相应地调整猜测。
发布尝试的问题在于代码(有几个类似的部分):
else if (randomNumber > guessNumber )
{for (int a = 1; a < 3; a++)
{
System.out.println(" Sorry Low number");
guessNumber = reading.nextInt();
}
System.out.println("Sorry! we can't help you any more, You failed *** The number was "+randomNumber);
}
因为这个循环从用户那里读取下一个猜测,但不会对randomNumber进行检查,但只是继续输出该数字太低。
您需要做的是重新考虑您的解决方案。一旦用户做出第二次猜测,代码需要再次检查它是否等于randomNumber,小于randomNumber或大于randomNumber。所以你的检查需要在里面循环,而不是检查中的循环。
while user has not made three guesses
get next guess from user
check guess and print appropriate messages