我为自己创建了一个简单的项目,现在我正在尝试集成一个代码,当我在控制台中输入“restart”时会使程序重新启动。为此,我在我的程序中创建了第二个方法,它包含与我的main方法完全相同的代码,但现在我想指定第二个方法,我不知道该怎么做。所有帮助表示赞赏! 这是我目前的代码:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("guess the number between 1 and 9");
int random = (int)(Math.random() * 9 + 1);
int value = 0;
do{
value = scanner.nextInt();
if (value != random){
System.out.println("Try again");
}
}
while(value != random);
System.out.println("You guessed the number");
if(value == random){
System.out.println("Would you like to restart?");
String reset = scanner.nextLine();
if (reset.equals("restart")){
/*
*I need some code here
*/
}
}
}
public static void restart(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("guess the number between 1 and 9");
int random = (int)(Math.random() * 9 + 1);
int value = 0;
do{
value = scanner.nextInt();
if (value != random){
System.out.println("Try again");
}
}
while(value != random);
System.out.println("You guessed the number");
if(value == random){
System.out.println("Would you like to restart?");
String reset = scanner.nextLine();
if (reset.equals("restart")){
}
}
}
答案 0 :(得分:1)
你应该在&#34中调用main(); if(reset.equals(" restart")){..."
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("guess the number between 1 and 9");
int random = (int)(Math.random() * 9 + 1);
int value = 0;
do{
value = scanner.nextInt();
if (value != random){
System.out.println("Try again");
}
}
while(value != random);
System.out.println("You guessed the number");
if(value == random){
System.out.println("Would you like to restart?");
String reset = scanner.nextLine();
if (reset.equals("restart")){
main(args); // restart you code
return;
}
}
}
或使用以下代码:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
do{
System.out.println("guess the number between 1 and 9");
int random = (int)(Math.random() * 9 + 1);
int value = 0;
do{
value = scanner.nextInt();
if (value != random){
System.out.println("Try again");
}
}
while(value != random);
System.out.println("You guessed the number");
System.out.println("Would you like to restart?");
String reset = scanner.nextLine();
}
while(reset.equals("restart"));
}
答案 1 :(得分:1)
尝试这样的事情,
在您的代码中,
- 你一直搞乱代码,需要按照要求分解,
- 此外,如果用户想要重新启动,则需要注意再次玩游戏。
醇>
通过以下代码克服了这一点,
static int value;
static int random = (int)(Math.random() * 9 + 1);
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
do{
System.out.println("Enter Your Guesses :");
value = scanner.nextInt();
if(value == random){
System.out.println("Exactly Match : " + value + " == " + random);
System.out.println("Would you like to restart?");
String reset = scanner.next();
if (reset.equals("restart")){
restart();
}
}else{
System.out.println("Try again");
}
}while(value != random);
System.out.println("You guessed the number");
}
public static void restart(){
do{
random = (int)(Math.random() * 9 + 1);
}while(value == random);
}