Java:不能再在我的应用程序中使用 System.out.println()
Java: Can no longer use System.out.println() in my application
我做了一个“猜随机数”小游戏,它接受用户的输入并检查它是否等于使用 for 循环生成的数字。如果是,应用程序将打印一条祝贺语句,然后跳出循环。如果否(并且失败五次),应用程序应该打印出游戏结束类型声明。
出于某种原因,我的 game over 语句没有打印出来,尽管所有其他 println() 语句都打印出来了。为什么它不起作用?
下面是我的代码:
import java.util.Random;
import java.util.Scanner;
public class randgame_draft {
public static void main(String[] args) {
System.out.println("Welcome to RandGame. What's your name?");
Scanner scanner = new Scanner(System.in);
String answer = scanner.next();
System.out.println("Hello, " + answer + ". Shall we begin?");
Scanner ask = new Scanner(System.in);
String response = ask.next();
if (response.equals("yes")) {
System.out.println("Alright!");
} else {
System.out.println("Oh, okay. Later!");
return;
}
Random random = new Random();
int rand = random.nextInt(20) + 1;
for (int i = 0; i < 5; i++) {
System.out.println(answer + ", please provide a random number between 1 and 20.");
Scanner begin = new Scanner(System.in);
int end = begin.nextInt();
System.out.println("you chose " + end);
if (end != rand && end > rand) {
System.out.println("Hint: guess lower");
} else if (end != rand && end < rand) {
System.out.println("Hint: guess higher");
} else if (end == rand) {
System.out.println("congratulations, you are correct!");
System.out.println("answer was " + rand);
System.out.println("thanks for playing, " + answer + "!");
return;
} else {
System.out.println("Sorry " + answer + ". the answer was " + rand + ". Game Over.");
}
}
}
}
看看你的条件:
1) end > rand //end != rand is redundant
2) end < rand
3) end == rand
4) else print
你能想象end不小于、大于或等于rand的任何场景吗?
你应该在循环结束并且玩家没有猜到数字后打印你的信息。
我做了一个“猜随机数”小游戏,它接受用户的输入并检查它是否等于使用 for 循环生成的数字。如果是,应用程序将打印一条祝贺语句,然后跳出循环。如果否(并且失败五次),应用程序应该打印出游戏结束类型声明。
出于某种原因,我的 game over 语句没有打印出来,尽管所有其他 println() 语句都打印出来了。为什么它不起作用?
下面是我的代码:
import java.util.Random;
import java.util.Scanner;
public class randgame_draft {
public static void main(String[] args) {
System.out.println("Welcome to RandGame. What's your name?");
Scanner scanner = new Scanner(System.in);
String answer = scanner.next();
System.out.println("Hello, " + answer + ". Shall we begin?");
Scanner ask = new Scanner(System.in);
String response = ask.next();
if (response.equals("yes")) {
System.out.println("Alright!");
} else {
System.out.println("Oh, okay. Later!");
return;
}
Random random = new Random();
int rand = random.nextInt(20) + 1;
for (int i = 0; i < 5; i++) {
System.out.println(answer + ", please provide a random number between 1 and 20.");
Scanner begin = new Scanner(System.in);
int end = begin.nextInt();
System.out.println("you chose " + end);
if (end != rand && end > rand) {
System.out.println("Hint: guess lower");
} else if (end != rand && end < rand) {
System.out.println("Hint: guess higher");
} else if (end == rand) {
System.out.println("congratulations, you are correct!");
System.out.println("answer was " + rand);
System.out.println("thanks for playing, " + answer + "!");
return;
} else {
System.out.println("Sorry " + answer + ". the answer was " + rand + ". Game Over.");
}
}
}
}
看看你的条件:
1) end > rand //end != rand is redundant
2) end < rand
3) end == rand
4) else print
你能想象end不小于、大于或等于rand的任何场景吗?
你应该在循环结束并且玩家没有猜到数字后打印你的信息。