如何检查循环 java 中的条件?
How do I check a condition within a loop java?
首先,我是 java 的新手,我想我应该先制作一些小程序来帮助自己学习。所以我开始了一个 "Don't Step the White Tile" 游戏,我得到了要打印的每一行的打印,但我需要检查用户输入是否等于图块。我不确定该怎么做,因为我已经尝试了很多东西,但遇到了障碍。
任何帮助,在此先感谢。
import java.util.Scanner;
public class DontStepTheWhiteTile {
public static void main(String[] args) {
//Variables
int userInput = 0, numberEntered = 0, tileNumber;
char start;
//Scanner
Scanner scanner = new Scanner(System.in);
//Loop for tiles
if (userInput == 0) {
System.out.print("Enter the character 's' to begin: ");
start = scanner.nextLine().charAt(0);
if (start == 's') {
userInput = 1;
}
}
//Starts the time
long timeStart = System.currentTimeMillis();
//Makes the tiles
if (userInput == 1){
for (int i = 0; i <= 25; i++) {
int tile = (int) (3 * Math.random() + 1);
if (tile == 1){
System.out.print("1 | X | X = ");
numberEntered = scanner.nextInt();
}
else if (tile == 2){
System.out.print("X | 2 | X = ");
numberEntered = scanner.nextInt();
}
else {
System.out.print("X | X | 3 = ");
numberEntered = scanner.nextInt();
}
}
}
//Calculates the ending time
long timeEnd = System.currentTimeMillis();
System.out.println("It took you "
+ ((timeEnd - timeStart) / 1000) + " seconds");
}
}
您从不测试 numberEntered
,因此用户输入不会改变代码的行为。如果您明确了自己想要的行为,那么回答您的问题会更容易。既然你不知道,我就只能猜测了。
如果您希望游戏在第一次未命中时结束,for
循环中的 break
将起作用。
if (numberEntered != tile) {
System.out.println("You lose");
break;
}
如果您希望游戏在 3 次未命中时结束,您在 break
之前测试的累积 int 将起作用。
if (numberEntered != tile) {
misses++;
}
if (misses >= 3) {
System.out.println("You lose");
break;
}
无论哪种方式,所有这些代码都可以放在当前 if-else
结构之后的 for
循环的末尾。
事实上 numberEntered = scanner.nextInt();
也可以移到 if-else
之后,因为它在每种情况下都是相同的。
首先,我是 java 的新手,我想我应该先制作一些小程序来帮助自己学习。所以我开始了一个 "Don't Step the White Tile" 游戏,我得到了要打印的每一行的打印,但我需要检查用户输入是否等于图块。我不确定该怎么做,因为我已经尝试了很多东西,但遇到了障碍。
任何帮助,在此先感谢。
import java.util.Scanner;
public class DontStepTheWhiteTile {
public static void main(String[] args) {
//Variables
int userInput = 0, numberEntered = 0, tileNumber;
char start;
//Scanner
Scanner scanner = new Scanner(System.in);
//Loop for tiles
if (userInput == 0) {
System.out.print("Enter the character 's' to begin: ");
start = scanner.nextLine().charAt(0);
if (start == 's') {
userInput = 1;
}
}
//Starts the time
long timeStart = System.currentTimeMillis();
//Makes the tiles
if (userInput == 1){
for (int i = 0; i <= 25; i++) {
int tile = (int) (3 * Math.random() + 1);
if (tile == 1){
System.out.print("1 | X | X = ");
numberEntered = scanner.nextInt();
}
else if (tile == 2){
System.out.print("X | 2 | X = ");
numberEntered = scanner.nextInt();
}
else {
System.out.print("X | X | 3 = ");
numberEntered = scanner.nextInt();
}
}
}
//Calculates the ending time
long timeEnd = System.currentTimeMillis();
System.out.println("It took you "
+ ((timeEnd - timeStart) / 1000) + " seconds");
}
}
您从不测试 numberEntered
,因此用户输入不会改变代码的行为。如果您明确了自己想要的行为,那么回答您的问题会更容易。既然你不知道,我就只能猜测了。
如果您希望游戏在第一次未命中时结束,for
循环中的 break
将起作用。
if (numberEntered != tile) {
System.out.println("You lose");
break;
}
如果您希望游戏在 3 次未命中时结束,您在 break
之前测试的累积 int 将起作用。
if (numberEntered != tile) {
misses++;
}
if (misses >= 3) {
System.out.println("You lose");
break;
}
无论哪种方式,所有这些代码都可以放在当前 if-else
结构之后的 for
循环的末尾。
事实上 numberEntered = scanner.nextInt();
也可以移到 if-else
之后,因为它在每种情况下都是相同的。