如何在输出为 false 时重复 "if" 语句
How to repeat "if" statement when output is false
我正在开发一个简单的游戏,用户必须在其中猜测一个随机数。我已经设置了所有代码,但如果猜测太高或太低,我不知道如何让他们重新输入数字并继续玩直到他们猜到。它只是停止;这是代码:
import java.util.Scanner;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int random = rand.nextInt(10) + 1;
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
if (number == random) {
System.out.println("Good!");
} else if (number > random) {
System.out.println("Too Big");
} else if (number < random) {
System.out.println("Too Small");
}
}
}
你可以使用 do...while
.
Random rand = new Random();
int random = rand.nextInt(10) + 1;
do {
Scanner input = new Scanner(System.in);
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
if (number == random) {
System.out.println("Good!");
} else if (number > random) {
System.out.println("Too Big");
} else if (number < random) {
System.out.println("Too Small");
}
} while ( number != random );
将 if 语句包含在一个 do-while 循环中,该循环会在用户还没有猜到数字时循环:
int number;
do {
System.out.print("Pick a number 1-10: ");
number = input.nextInt();
if (number == random) {
System.out.println("Good!");
} else if (number > random) {
System.out.println("Too Big");
} else if (number < random) {
System.out.println("Too Small");
}
} while (number != random);
您需要为此使用 loop。代码应该像这样工作:
import java.util.Scanner;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int random = rand.nextInt(10) + 1;
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
boolean found = false;
while (!found) {
if (number == random) {
System.out.println("Good!");
found = true;
} else if (number > random) {
System.out.println("Too Big, try again:");
number = input.nextInt();
} else if (number < random) {
System.out.println("Too Small, try again:");
number = input.nextInt();
}
}
}
}
为了重复任何内容,您需要一个循环。
重复直到循环体中间的条件得到满足的一种常见方法是构建一个无限循环,并添加一种跳出它的方法。
在 Java 中进行无限循环的惯用方法是 while(true)
:
while (true) {
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
if (number == random) {
System.out.println("Good!");
break; // This ends the loop
} else if (number > random) {
System.out.println("Too Big");
} else if (number < random) {
System.out.println("Too Small");
}
}
此循环将继续迭代,直到代码路径到达 break
语句。
您正在寻找的是允许您一次又一次地做特定事情的编程语言结构。
这是使用循环完成的。
例如检查 docs for the while loop。这就是你需要的。
为了有条件地重复代码,使用循环。
// this code will execute only once
System.out.print("Pick a number 1-10: ");
// initialize number to a value that would not be used and not equal random
int number = -1;
// the code inside the curly braces will repeat until number == random
while (number != random) {
// get next number
number = input.nextInt();
// handle case one
if(number > random) System.out.println("Too Big");
// handle case two
if(number < random) System.out.println("Too Small");
}
// once number == random, the condition is false so we break out of the loop
System.out.println("Good!");
有几种技术可以循环您的请求,其中包括:
while (<condition>) { <do something> }
do { <something> } while (<condition>);
for (<init statement>, <condition>, <update statement>) { <do something> }
为了炫耀,您可以通过使用递归来避免使用上述显式循环结构之一:
mport java.util.Scanner;
import java.util.Random;
public class Test {
public static void ask(int random) {
Scanner input = new Scanner(System.in);
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
if (number == random) {
System.out.println("Good!");
} else if (number > random) {
System.out.println("Too Big");
ask(random);
} else if (number < random) {
System.out.println("Too Small");
ask(random);
}
}
public static void main(String[] args) {
Random rand = new Random();
int random = rand.nextInt(10) + 1;
ask(random);
}
}
此处ask()
方法不断调用自身,直到达到结束条件(用户猜对)。
根据 Java 虚拟机的聪明程度,这可能会对调用堆栈造成压力,也可能不会。
我正在开发一个简单的游戏,用户必须在其中猜测一个随机数。我已经设置了所有代码,但如果猜测太高或太低,我不知道如何让他们重新输入数字并继续玩直到他们猜到。它只是停止;这是代码:
import java.util.Scanner;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int random = rand.nextInt(10) + 1;
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
if (number == random) {
System.out.println("Good!");
} else if (number > random) {
System.out.println("Too Big");
} else if (number < random) {
System.out.println("Too Small");
}
}
}
你可以使用 do...while
.
Random rand = new Random();
int random = rand.nextInt(10) + 1;
do {
Scanner input = new Scanner(System.in);
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
if (number == random) {
System.out.println("Good!");
} else if (number > random) {
System.out.println("Too Big");
} else if (number < random) {
System.out.println("Too Small");
}
} while ( number != random );
将 if 语句包含在一个 do-while 循环中,该循环会在用户还没有猜到数字时循环:
int number;
do {
System.out.print("Pick a number 1-10: ");
number = input.nextInt();
if (number == random) {
System.out.println("Good!");
} else if (number > random) {
System.out.println("Too Big");
} else if (number < random) {
System.out.println("Too Small");
}
} while (number != random);
您需要为此使用 loop。代码应该像这样工作:
import java.util.Scanner;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int random = rand.nextInt(10) + 1;
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
boolean found = false;
while (!found) {
if (number == random) {
System.out.println("Good!");
found = true;
} else if (number > random) {
System.out.println("Too Big, try again:");
number = input.nextInt();
} else if (number < random) {
System.out.println("Too Small, try again:");
number = input.nextInt();
}
}
}
}
为了重复任何内容,您需要一个循环。
重复直到循环体中间的条件得到满足的一种常见方法是构建一个无限循环,并添加一种跳出它的方法。
在 Java 中进行无限循环的惯用方法是 while(true)
:
while (true) {
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
if (number == random) {
System.out.println("Good!");
break; // This ends the loop
} else if (number > random) {
System.out.println("Too Big");
} else if (number < random) {
System.out.println("Too Small");
}
}
此循环将继续迭代,直到代码路径到达 break
语句。
您正在寻找的是允许您一次又一次地做特定事情的编程语言结构。
这是使用循环完成的。 例如检查 docs for the while loop。这就是你需要的。
为了有条件地重复代码,使用循环。
// this code will execute only once
System.out.print("Pick a number 1-10: ");
// initialize number to a value that would not be used and not equal random
int number = -1;
// the code inside the curly braces will repeat until number == random
while (number != random) {
// get next number
number = input.nextInt();
// handle case one
if(number > random) System.out.println("Too Big");
// handle case two
if(number < random) System.out.println("Too Small");
}
// once number == random, the condition is false so we break out of the loop
System.out.println("Good!");
有几种技术可以循环您的请求,其中包括:
while (<condition>) { <do something> }
do { <something> } while (<condition>);
for (<init statement>, <condition>, <update statement>) { <do something> }
为了炫耀,您可以通过使用递归来避免使用上述显式循环结构之一:
mport java.util.Scanner;
import java.util.Random;
public class Test {
public static void ask(int random) {
Scanner input = new Scanner(System.in);
System.out.print("Pick a number 1-10: ");
int number = input.nextInt();
if (number == random) {
System.out.println("Good!");
} else if (number > random) {
System.out.println("Too Big");
ask(random);
} else if (number < random) {
System.out.println("Too Small");
ask(random);
}
}
public static void main(String[] args) {
Random rand = new Random();
int random = rand.nextInt(10) + 1;
ask(random);
}
}
此处ask()
方法不断调用自身,直到达到结束条件(用户猜对)。
根据 Java 虚拟机的聪明程度,这可能会对调用堆栈造成压力,也可能不会。