在每个循环结束后减去1。

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

Substract 1 after each loop end

问题

我是你的中文翻译,以下是已翻译的内容:

我刚开始学习Java,尝试制作一个猜数字游戏。

当循环开始时,我想检查用户是否还有尝试机会。

if (remain > 1);

然后在每次循环结束时,我想从尝试次数中减去1。
我还尝试在每次循环结束后进行累加。

byte tries = 5, remain = (byte)(--tries);

当用户没有尝试次数时,我想终止循环并结束游戏:

else if (remain == 0){
     System.out.println("你没有猜中数字!");
     break;
   }

以下是你的代码:

import java.util.Scanner;

public class Master{
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        double number = (byte)(Math.random() * 21);

        while(true){
            // 用户输入
            System.out.print("选择一个1到21之间的数字:");

            byte user = scanner.nextByte();
            byte tries = 5, remain = (byte)(--tries);
            System.out.println(remain);
          
            if (remain > 1);
                // 游戏逻辑
                if (user == number){
                    System.out.println("你猜中了数字!");
                    break;
                } else if (user < number)
                    System.out.println("你猜的数字太小!");
                else if (user > number)
                    System.out.println("你猜的数字太大!");

            // 如果用户没有尝试次数,则终止循环
            else if (remain == 0){
                System.out.println("你没有猜中数字!");
                break;
            }
            
        }
    }
}

代码运行得很好,但当用户没有尝试机会时,它不会终止循环。

英文:

I'm new at java and i tried to make a Guess The Number game.

When the loop starts, i want to check if the user has any tries left.

if (remain &gt; 1);

then after each end of the loop I want to subtract 1 from the tries.
I also tried to gather 1 after each loop ends.

byte tries = 5, remain=(byte)(--tries);

When user is out of tries i want to break the loop and break the game:

else if (remain == 0){
     System.out.println(&quot;You haven&#39;t guessed the number!&quot;);
     break;
   }

Here's my code:


public class Master{
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        double number = (byte)(Math.random() * 21);

        while(true){
            // User input
            System.out.print(&quot;Choose a number between 1 and 21 :&quot;);

            byte user = scanner.nextByte();
            byte tries = 5, remain=(byte)(--tries);
            System.out.println(remain);
          
            if (remain &gt; 1);
                //game
                if (user==number){
                    System.out.println(&quot;You have guessed the number!&quot;);
                    break;
                } else if (user &lt; number)
                    System.out.println(&quot;You are lower than the number!&quot;);
                else if (user &gt; number)
                    System.out.println(&quot;You are higher than the number!&quot;);

            //Break if user is out of tries
            else if (remain == 0){
                System.out.println(&quot;You haven&#39;t guessed the number!&quot;);
                break;
            }
            
        }
    }
}

Code works perfectly but it won't break when user is out of tries.

答案1

得分: 1

tries变量应在while循环外部进行初始化,if语句的关闭应使用';'。

private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
    double number = (byte)(Math.random() * 21);
    byte tries = 5;  // 将tries的初始化移至循环外部
    while(true){
        // 用户输入
        System.out.print("选择一个1到21之间的数字:");

        byte user = scanner.nextByte();
    
        byte remain=(byte)(--tries);
        System.out.println(remain);
      
        if (remain > 1) { // 这里你使用了;,if语句被关闭了
            // 游戏逻辑
            if (user==number){
                System.out.println("你猜对了数字!");
                break;
            } else if (user < number)
                System.out.println("你猜的数字偏小!");
            else if (user > number)
                System.out.println("你猜的数字偏大!");
        }
        // 如果用户用完了尝试次数,则跳出循环
        else if (remain == 0){
            System.out.println("你没有猜中数字!");
            break;
        }
           
    }
}
英文:

tries variable should be initialized outside the while loop and the if statement was being closed using a ';'

private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        double number = (byte)(Math.random() * 21);
        byte tries = 5;  // tries should be initialized outside the loop
        while(true){
            // User input
            System.out.print(&quot;Choose a number between 1 and 21 :&quot;);

            byte user = scanner.nextByte();
        
            byte remain=(byte)(--tries);
            System.out.println(remain);
          
            if (remain &gt; 1) { // You were using ; and this if statement was closed
                //game
                if (user==number){
                    System.out.println(&quot;You have guessed the number!&quot;);
                    break;
                } else if (user &lt; number)
                    System.out.println(&quot;You are lower than the number!&quot;);
                else if (user &gt; number)
                    System.out.println(&quot;You are higher than the number!&quot;);
            }
            //Break if user is out of tries
            else if (remain == 0){
                System.out.println(&quot;You haven&#39;t guessed the number!&quot;);
                break;
            }
               
        }
    }

答案2

得分: 1

我觉得你对很多概念有误解。以下是带注释的版本:

public class Master {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        // 要找的数字
        double numberToFound = (byte)(Math.random() * 21);
        // 用户输入的数字将被存储在这里
        byte userInput = 0;
        // 剩余的尝试次数
        byte tryLeft = (byte) 5;
        
        do { 
            // 询问用户一个数字
            System.out.print("选择一个介于 1 和 21 之间的数字:");
            userInput = scanner.nextByte(); // 存储用户输入
            
            // 检测数字
            if (userInput > numberToFound) {
                System.out.println("你猜的数字偏小!");
            } else {
                System.out.println("你猜的数字偏大!");
            }
            tryLeft--; // 减少一次尝试机会
        } while(tryLeft > 0 && userInput != numberToFound); // 循环直到没有尝试机会或者找到了数字

        // 最后一次检查(检查循环退出的原因)
        // 用户找到了数字
        if (numberToFound == userInput)
            System.out.println("你猜对了!");
        else // 用户没有剩余尝试次数
            System.out.println("你没有猜对!");
    }
}

如果有终止条件,应避免使用 while(true),以提高可读性。

英文:

I think you are misunderstanding a lot of concepts. Here's a commented version :

public class Master {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        // The number to found
        double numberToFound = (byte)(Math.random() * 21);
        // The user number will be stored here
        byte userInput = 0;
        // The number of tries left
        byte tryLeft = (byte) 5;
        
        do { 
            // Ask the User an number
            System.out.print(&quot;Choose a number between 1 and 21 :&quot;);
            userInput = scanner.nextByte(); // Store it
            
            // Test the number
            if (userInput &gt; numberToFound) {
                System.out.println(&quot;You are lower than the number!&quot;);
            } else {
                System.out.println(&quot;You are higher than the number!&quot;);
            }
            tryLeft--; // We remove one try
        } while(tryLeft &gt; 0 &amp;&amp; userInput != numberToFound); // We loop until there are no more try OR we found the number

        // Last time check (we check why we exited the loop)
        // The user found the number
        if (numberToFound == userInput)
            System.out.println(&quot;You have guessed the number!&quot;);
        else // The user has no more tries left
            System.out.println(&quot;You haven&#39;t guessed the number!&quot;);
    }
}

You should avoid using while(true) if you have a breaking condition for better readability.

答案3

得分: 0

你可以尝试这段代码。

我更改了一些数据类型并在循环内部减少了remain变量。

public class Master{
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int number = (int)(Math.random() * 21);
        
        int tries = 5;
        int remain = tries;
        
        while(true){
            System.out.print("选择一个1到21之间的数字:");
            int user = scanner.nextInt();

            // 用户输入
            System.out.println(remain);
          
            if (remain > 1) {
                // 游戏
                if (user==number){
                    System.out.println("你猜对了!");
                    break;
                } else if (user < number)
                    System.out.println("你猜的数字太小了!");
                else if (user > number)
                    System.out.println("你猜的数字太大了!");
                --remain;
            }
            // 如果用户用完所有尝试机会就退出
            else if (remain == 0){
                System.out.println("你没有猜对!");
                break;
            }
            
        }
    }
}      
英文:

You can try this code.

I changed some datatypes and added the decreasing of the remain variable inside the loop.

public class Master{
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int number = (int)(Math.random() * 21);
        
        int tries = 5;
        int remain = tries;
        
        while(true){
            System.out.print(&quot;Choose a number between 1 and 21:&quot;);
            int user = scanner.nextInt();

            // User input
            System.out.println(remain);
          
            if (remain &gt; 1) {
                //game
                if (user==number){
                    System.out.println(&quot;You have guessed the number!&quot;);
                    break;
                } else if (user &lt; number)
                    System.out.println(&quot;You are lower than the number!&quot;);
                else if (user &gt; number)
                    System.out.println(&quot;You are higher than the number!&quot;);
                --remain;
            }
            //Break if user is out of tries
            else if (remain == 0){
                System.out.println(&quot;You haven&#39;t guessed the number!&quot;);
                break;
            }
            
        }
    }
}      

答案4

得分: 0

首先,您可以检查一下 "remain" 是否大于 0 且不等于 1,您的代码将会按照您计划的方式执行(在 else 部分无需再检查 == 0) 在每个循环结束后减去1。

其次,为了使您的代码更有条理,您可以添加一些简短的注释:

  1. 在编写代码时,使用您熟悉的参数类型,比如 int(更易于调试和操作),只有在代码真正需要时才使用 byte。
  2. 将只用于检查的参数移到循环外部,比如 tries(请考虑,您将在每次循环迭代中定义此变量,而实际上不需要这样做)。
  3. 对于您不打算更改的参数(比如移至循环外部的 tries 和 number),添加 final 修饰符,以便更清楚地表明您不会再更改此参数。

除此之外,看起来非常不错,祝好运! 在每个循环结束后减去1。

英文:

First of all, you can check if the "remain" is bigger than 0 and not 1, and your code will do the job as you planned (and you don't need to check == 0 at the else) 在每个循环结束后减去1。

Second of all small comments to make your code more arranged:

  1. Work with parameters that are comfortable to you like int (easier to debug and work with) you should use byte only if the code really needs it.
  2. Move parameters that you use only to check them outside the loop like tries (think on that you will define this variable on every loop iteration and you don't need to).
  3. Add to the params you are not going to change (like tries & number after you moved outside the loop) final it will be more clear that you are not going to change this param anymore.

Other than that looks great, good luck! 在每个循环结束后减去1。

huangapple
  • 本文由 发表于 2020年10月6日 17:07:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64222653.html
匿名

发表评论

匿名网友

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

确定