如果用户输入错误的数字或如果用户输入“1”则重新开始,我该如何做到这一点
How do I make it so that it stars over if the user inputs wrong number OR start over if user types in "1"
package w3school;
import java.util.Random;
import java.util.Scanner;
public class nyttprogram {
static void indata() {
{
Scanner determinedNumber = new Scanner(System.in);
int user, computer, number, user2;
System.out.println("Input a number from 0-10");
user = determinedNumber.nextInt();
Random random = new Random();
int randomInt = random.nextInt(10);
if (user == randomInt) {
System.out.println("You guessed the correct number!");
} else {
System.out.println("You guessed the wrong number");
System.out.println("The correct number was: " + randomInt);
}
System.out.println("Input 1 if you want to try again: ");
}
}
public static void main(String[] args) {
indata();
}
}
如何让 class 在用户输入 1 时重新开始,或者如果 Class 可以在用户从一开始输入错误的数字时重新开始,非常感谢
How do I make the class start over when user input 1 OR if the Class can start over if User inputs wrong number from the start, many thanks
- 基于某些条件的"start over"逻辑通常用
while
和do/while
循环实现。
- 首先让我们提取这些条件。如果出现以下情况,我们想再次迭代(重新开始):
- 用户猜测错误
- 用户的猜测是正确的,但当询问他们是否要继续时,他们输入的数字与
1
不同。
- 因为我们想 运行 程序 至少一次 ,自然的方法是
do/while
。这将 运行 一次迭代,然后检查所需的条件。
这是它的样子:
private static void inData() {
Scanner userInputScanner = new Scanner(System.in);
Random random = new Random();
// Declare the stop/continue condition
boolean isLoopContinue;
do {
// Generate a random number
int expectedNumber = random.nextInt(10);
// Ask the user to guess a number
System.out.println("Input a number from 0-10");
int givenNumber = userInputScanner.nextInt();
if (givenNumber == expectedNumber) {
// Correct answer, check if the user wants to continue
System.out.println("You guessed the correct number!");
System.out.println("\nInput 1 if you want to try again: ");
// If they input "1", then we continue. Else we stop
isLoopContinue = userInputScanner.nextInt() == 1;
} else {
// Wrong answer, loop again
System.out.println("You guessed the wrong number");
System.out.println("The correct number was: " + expectedNumber);
isLoopContinue = true;
}
} while (isLoopContinue);
}
package w3school;
import java.util.Random;
import java.util.Scanner;
public class nyttprogram {
static void indata() {
{
Scanner determinedNumber = new Scanner(System.in);
int user, computer, number, user2;
System.out.println("Input a number from 0-10");
user = determinedNumber.nextInt();
Random random = new Random();
int randomInt = random.nextInt(10);
if (user == randomInt) {
System.out.println("You guessed the correct number!");
} else {
System.out.println("You guessed the wrong number");
System.out.println("The correct number was: " + randomInt);
}
System.out.println("Input 1 if you want to try again: ");
}
}
public static void main(String[] args) {
indata();
}
}
如何让 class 在用户输入 1 时重新开始,或者如果 Class 可以在用户从一开始输入错误的数字时重新开始,非常感谢
How do I make the class start over when user input 1 OR if the Class can start over if User inputs wrong number from the start, many thanks
- 基于某些条件的"start over"逻辑通常用
while
和do/while
循环实现。 - 首先让我们提取这些条件。如果出现以下情况,我们想再次迭代(重新开始):
- 用户猜测错误
- 用户的猜测是正确的,但当询问他们是否要继续时,他们输入的数字与
1
不同。
- 因为我们想 运行 程序 至少一次 ,自然的方法是
do/while
。这将 运行 一次迭代,然后检查所需的条件。
这是它的样子:
private static void inData() {
Scanner userInputScanner = new Scanner(System.in);
Random random = new Random();
// Declare the stop/continue condition
boolean isLoopContinue;
do {
// Generate a random number
int expectedNumber = random.nextInt(10);
// Ask the user to guess a number
System.out.println("Input a number from 0-10");
int givenNumber = userInputScanner.nextInt();
if (givenNumber == expectedNumber) {
// Correct answer, check if the user wants to continue
System.out.println("You guessed the correct number!");
System.out.println("\nInput 1 if you want to try again: ");
// If they input "1", then we continue. Else we stop
isLoopContinue = userInputScanner.nextInt() == 1;
} else {
// Wrong answer, loop again
System.out.println("You guessed the wrong number");
System.out.println("The correct number was: " + expectedNumber);
isLoopContinue = true;
}
} while (isLoopContinue);
}