Java:无法在我的应用程序中继续使用System.out.println()。

huangapple go评论90阅读模式
英文:

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(&quot;Welcome to RandGame. What&#39;s your name?&quot;);
Scanner scanner = new Scanner(System.in);
String answer = scanner.next();
System.out.println(&quot;Hello, &quot; + answer + &quot;. Shall we begin?&quot;);
Scanner ask = new Scanner(System.in);
String response = ask.next();
if (response.equals(&quot;yes&quot;)) {
System.out.println(&quot;Alright!&quot;);
} else {
System.out.println(&quot;Oh, okay. Later!&quot;);
return;
}
Random random = new Random();
int rand = random.nextInt(20) + 1;
for (int i = 0; i &lt; 5; i++) {
System.out.println(answer + &quot;, please provide a random number between 1 and 20.&quot;);
Scanner begin = new Scanner(System.in);
int end = begin.nextInt();
System.out.println(&quot;you chose &quot; + end);
if (end != rand &amp;&amp; end &gt; rand) {
System.out.println(&quot;Hint: guess lower&quot;);
} else if (end != rand &amp;&amp; end &lt; rand) {
System.out.println(&quot;Hint: guess higher&quot;);
} else if (end == rand) {
System.out.println(&quot;congratulations, you are correct!&quot;);
System.out.println(&quot;answer was &quot; + rand);
System.out.println(&quot;thanks for playing, &quot; + answer + &quot;!&quot;);
return;
} else {
System.out.println(&quot;Sorry &quot; + answer + &quot;. the answer was &quot; + rand + &quot;. Game Over.&quot;);
}
}
}
}

答案1

得分: 5

看看你的条件:

1) end &gt; rand //end != rand  is redundant
2) end &lt; rand
3) end == rand
4) else print

你能想象到任何情况下 end 不小于、大于或等于 rand 吗?

你应该在循环结束并且玩家没有猜对数字时打印你的消息。

英文:

Have a look at your conditions:

1) end &gt; rand //end != rand  is redundant
2) end &lt; 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.

huangapple
  • 本文由 发表于 2020年7月28日 03:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63122548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定