英文:
Java: Can no longer use System.out.println() in my application
问题
Here's the translated portion of your code:
import java.util.Random;
import java.util.Scanner;
public class randgame_draft {
public static void main(String[] args) {
System.out.println("欢迎来到RandGame。你叫什么名字?");
Scanner scanner = new Scanner(System.in);
String answer = scanner.next();
System.out.println("你好," + answer + "。我们开始吧?");
Scanner ask = new Scanner(System.in);
String response = ask.next();
if (response.equals("是")) {
System.out.println("好的!");
} else {
System.out.println("哦,好的。再见!");
return;
}
Random random = new Random();
int rand = random.nextInt(20) + 1;
for (int i = 0; i < 5; i++) {
System.out.println(answer + ",请提供一个介于1和20之间的随机数。");
Scanner begin = new Scanner(System.in);
int end = begin.nextInt();
System.out.println("你选择了" + end);
if (end != rand && end > rand) {
System.out.println("提示:猜小一点");
} else if (end != rand && end < rand) {
System.out.println("提示:猜大一点");
} else if (end == rand) {
System.out.println("恭喜你,答对了!");
System.out.println("答案是" + rand);
System.out.println("感谢参与," + answer + "!");
return;
} else {
System.out.println("抱歉" + answer + ",答案是" + rand + "。游戏结束。");
}
}
}
}
Please note that I've translated the code comments and user prompts into Chinese.
英文:
I made a little "guess the random number" game that takes the user's input and checks if it is equal to the number generated using a for loop. If yes, the application prints a congratulations statement and then breaks out of the loop. If no (and failed five times), the application is supposed to print out a game over-type statement.
For some reason, my game over statement does not print, even though all the other println() statements do. Why isn't it working?
Below is my code:
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
得分: 5
看看你的条件:
1) end > rand //end != rand is redundant
2) end < rand
3) end == rand
4) else print
你能想象到任何情况下 end 不小于、大于或等于 rand 吗?
你应该在循环结束并且玩家没有猜对数字时打印你的消息。
英文:
Have a look at your conditions:
1) end > rand //end != rand is redundant
2) end < rand
3) end == rand
4) else print
Can you imagine any scenario where end is not smaller, greater or equal to rand?
You should print your message after the loop finishes and the player didn’t guess the number.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论