【问题标题】:Can I give a condition to boolean primitive as initialize?我可以给布尔基元一个条件作为初始化吗?
【发布时间】:2019-04-18 07:00:42
【问题描述】:

我不确定这是否可能,因为我还没有找到确切的答案,但 NetBeans 没有给出错误。但如果可能的话,为什么我的代码不起作用?

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    int[][] fiveMatrix = {
        {1, 4, 7, 5, 3}, {3, 7, 9, 10, 1}, {4, -3, 2, -4, 1}, {5, 9, 6, 4, 3}, {1, 2, 3, 4, 5},};

    System.out.print("Which line do you want to write out (0-4)? ");
    int lineNumber = scan.nextInt();
    boolean goodLine = lineNumber < 0 || lineNumber > 4;
    if (goodLine) {
        while (goodLine) {
            System.out.println("Bad index.");
            System.out.print("Which line do you want to write out (0-4)? ");
            lineNumber = scan.nextInt();
        }
    }
}

}

【问题讨论】:

  • 你能准确解释什么“不起作用”吗?没有错误消息或堆栈跟踪很难判断。
  • while循环内添加goodLine = lineNumber &lt; 0 || lineNumber &gt; 4;退出。
  • @secretsuperstar 这看起来是一个简单的解决方案,但它在概念上是错误的:你不能复制代码,即使它只是一行。因为您必须记住,当需要适应条件时,您必须更新支票的每个副本。复制代码始终是引入未来错误的第一步。
  • @dave 你是对的。我没有提到错误信息。它进入了一个无限循环。后来我删除了 if 条件,但这也没有帮助。
  • @GhostCat 谢谢你的笔记。我正在努力理解答案。 :) 我完全是个新手。我当然会接受其中一种解决方案。

标签: java while-loop boolean


【解决方案1】:

这里是:

boolean goodLine = lineNumber < 0 || lineNumber > 4;

被评估一次,并将结果分配给该变量。

稍后对lineNumber = scan.nextInt(); 的更改更改该布尔变量!

“正确”的解决方案:您必须重新计算布尔属性。但理想情况下不是通过复制代码,而是通过创建一个小的帮助方法:

boolean isGoodLine(int lineNumber) { return lineNumber < 0 || lineNumber > 4; }

现在,您无需在其他代码中使用布尔变量,只需在 lineNumber 更改时调用该方法!

【讨论】:

    【解决方案2】:

    您可能希望更新循环内的布尔值,以避免无限循环。

    boolean badLine = lineNumber < 0 || lineNumber > 4;
    while (badLine) {
        System.out.println("Bad index.");
        System.out.print("Which line do you want to write out (0-4)? ");
        lineNumber = scan.nextInt();
        badLine = lineNumber < 0 || lineNumber > 4;
    }
    

    我重命名了boolean,因为它的原始名称令人困惑。我还删除了if 条件,因为它是多余的。

    【讨论】:

    • 谢谢。重命名建议是完全合理的。而且我真的忘了告诉 while 循环每次都要检查条件。
    【解决方案3】:

    您在 while 循环中缺少一行

    boolean goodLine = lineNumber < 0 || lineNumber > 4;
    

    考虑重构方法以避免重复代码:

    public boolean goodLine(Scanner scan) {
        System.out.print("Which line do you want to write out (0-4)? ");
        int lineNumber = scan.nextInt();
        return lineNumber < 0 || lineNumber > 4;
    }
    

    并称它为:

    while(goodLine());
    

    考虑也称它为badLine,因为用户输入错误(不是 0-4 值)

    【讨论】:

    • 谢谢。完美的解决方案。我选择了另一个,因为它更短。
    【解决方案4】:

    代码简洁易懂

    import java.util.Scanner;
    
    public class Post3 {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
    
            int[][] fiveMatrix = {
                {1, 4, 7, 5, 3}, {3, 7, 9, 10, 1}, {4, -3, 2, -4, 1}, {5, 9, 6, 4, 3}, {1, 2, 3, 4, 5},};
    
            System.out.println("Which line do you want to write out (0-4)? ");
            int input = -1;
            while(true) {
                System.out.println("input valid number between 0 to 4");
                input = scan.nextInt();
                if(input >= 0 && input <= 4) {
                    break;
                }
            }
            System.out.println("input is "+input);
            scan.close();
    
        }
    
    }
    

    【讨论】:

    • 收好。谢谢你。您的解决方案带来了新的想法。另一方面不处理第一个数据请求。如果可以处理,这将是我想要避免的代码重复。
    • 它确实可以处理。它将一直持续到用户输入有效输入!你想接受两个输入吗?
    • 我的措辞不准确。我需要一个 'System.out.println("Bad index.");'错误号码的错误代码(因为它在问题内部)。如果我在 while 循环中使用,它会被读出,无论答案是否好。这就是为什么我需要在循环之前请求数据。
    猜你喜欢
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多