[英]If else statement is not working properly
这是一个随机数猜谜游戏。 如果我得到正确答案,我的 if/else 语句将不起作用。 我还想给用户三个机会来得到正确的答案。 我是编程新手,不知道该怎么做。 任何帮助表示赞赏。
package FinalProject;
import java.util.Random;
import java.util.Scanner;
public class test {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a nickname");
String name = s.nextLine();
Scanner kbReader = new Scanner(System.in);
System.out.println("Enter a number between 1 and 15");
int number = kbReader.nextInt();
Random randomNumber = new Random();
int[] arr = new int[1];
for (int x = 0; x < arr.length; x++) {
arr[x] = (int) (Math.random() * 15);
System.out.println("The number is:" + arr[x]);
int i = 0;
String quit;
do {
if (x == number) {
System.out.println("You win! " + name);
} else {
System.out.println("You lose " + name);
}
System.out.println("To quit the program enter q ");
Scanner q = new Scanner(System.in);
quit = q.nextLine();
} while (!quit.equals(("q")));
}
}
}
你需要使用
if(arr[x] == number)
而不是if(x == number)
在这里 x 是 0 不等于你的 randomNumber 而 arr[x] 可以。
你的代码似乎很复杂。 我知道你得到一个随机数,你让用户猜三遍。 然后如果错了,用户可以退出或者用一个新的随机数开始一个新的游戏,然后再尝试3次
检查以下代码。 我解释了代码下面的大部分块。
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a nickname");
String name = s.nextLine();
while(true) {
int tries=0;
int randomNumber = (int) (Math.random() * 15)+1;
while (tries < 3) {
System.out.println("Enter a number between 1 and 15");
int number = Integer.parseInt(s.nextLine());
if (randomNumber == number) {
System.out.println("You win! " + name);
break;
} else {
System.out.println("You lose " + name);
}
tries++;
}
System.out.println("The number is:" + randomNumber);
System.out.println("To quit the program enter q ");
String quit = s.nextLine();
if ("q".equals(quit)) {
break;
}
}
}
通常,您可以使用一个 Scanner 来读取每个用户的输入。 当 nextInt 读取到下一个标记时,最好一次读取一行。 而不是 nextInt() 执行 nextLine() 并将字符串解析为 int -> int number = Integer.parseInt(s.nextLine());
我无法理解数组的使用,在我的示例中,我使用了每次猜测时都会增加的尝试 integer。 While 循环一直运行,直到尝试次数等于 3,因此用户已完成尝试 0、1、2(3 次尝试)。
外部循环while(true)
永远运行游戏,直到用户输入“q”,循环中断并且游戏结束。
在游戏的 while 循环中,从 Math.random() 中获取一个 randomNumber,并在 if else 中针对该特定输入验证用户的输入。 请注意,如果您想获得 1-15 之间的随机数,您应该使用int randomNumber = (int) (Math.random() * 15)+1;
因为 Math.random() * 15 返回一个 0-14 之间的随机数。
如果猜到正确的数字,您不希望游戏要求下一次猜测,所以添加一个break;
在“你赢” System.out 之后。 这样内部循环就结束了,用户被要求退出或不退出。
如果尝试次数增加到 3,则游戏会打印正确的数字并要求用户输入 q 以退出程序。 你阅读了这一行,如果"q".equals(quit)
你打破了外循环并退出。 如果用户按下任何其他文本,则外循环继续,尝试设置为 0,采用新的随机数,依此类推。
更新:代码更改为支持一个随机数中的 3 次猜测,并在 3 次猜测错误后退出消息。
Output 有 3 个错误猜测:
Enter a nickname
joe
Enter a number between 1 and 15
2
You lose joe
Enter a number between 1 and 15
3
You lose joe
Enter a number between 1 and 15
4
You lose joe
The number is:12
To quit the program enter q
q
Output 猜测正确:
Enter a nickname
Joe
Enter a number between 1 and 15
1
You lose Joe
Enter a number between 1 and 15
2
You win! Joe
The number is:2
To quit the program enter q
您提到数组的大小为1
,因此我建议您不要使用数组; 请改用非数组变量。 然而,如果你想使用一个数组,你应该比较if(arr[x] == number)
。
看起来您希望程序的行为如下:
import java.util.Scanner;
public class Test {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.print("Enter a nickname: ");
String name = s.nextLine();
String quit = "";
int number, x, count=1;
do {
System.out.print("Enter a number between 1 and 15: ");
number = s.nextInt();
x = (int) (Math.random() * 15);
System.out.println("The random number is: " + x);
if (x == number) {
System.out.println("You win, " + name+"!");
} else {
System.out.println("You lose, " + name+"!");
}
if (count==3)
break;
count++;
System.out.print("To quit the program enter q: ");
s = new Scanner(System.in);
quit = s.nextLine();
} while (!quit.equals(("q")));
}
}
示例运行:
Enter a nickname: John
Enter a number between 1 and 15: 2
The random number is: 1
You lose, John!
To quit the program enter q: w
Enter a number between 1 and 15: 3
The random number is: 3
You win, John!
To quit the program enter q: t
Enter a number between 1 and 15: 3
The random number is: 2
You lose, John!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.