【问题标题】:Trouble with using a simple boolean [closed]使用简单布尔值的麻烦[关闭]
【发布时间】:2013-05-09 03:53:57
【问题描述】:

对不起,如果这是一个愚蠢的问题,但我对此完全陌生,只是尝试一下。 我的代码卡住了....此后,无论选择 1 还是 2,程序都会结束。我知道我缺少一些简单的东西....任何输入表示赞赏。我已经复制并粘贴了我认为问题所在的部分。

    System.out.println("Is this information correct? Enter 1 if it is correct, and 2 to change");
    Scanner inputCorrect = new Scanner(System.in);
    int pick = inputCorrect.nextInt();
    boolean isCorrect = false;

    while (isCorrect = false){

          while (!(pick == 1) && (!(pick ==2)))
              System.out.println("That is not a valid entry please try again");

              if (pick == 1){

                  isCorrect = true;
              }

              if (pick == 2){
                  System.out.println("Enter 1 to change your name, 2 to change your age or 3 to change your gender");
                  Scanner inputChange = new Scanner(System.in);
                  int change = inputChange.nextInt();

                    if (change ==1){
                        Scanner inputNewName = new Scanner(System.in);
                        System.out.println("Enter the correct name: ");
                        String correctedName = inputNewName.next();
                        you.setName(correctedName);
                        System.out.println(you);
                        isCorrect = true;
                    }

                    if (change ==2) {
                        Scanner inputNewAge = new Scanner(System.in);
                        System.out.println("Enter the correct age: ");
                        int correctedAge = inputNewAge.nextInt();
                        you.setAge(correctedAge);
                        System.out.println(you);
                        isCorrect = true;

                    }

                    if (change == 3) {
                        Scanner inputNewGender = new Scanner (System.in);
                        System.out.println("Enter the correct gender: ");
                        char correctedGender = inputNewGender.next().charAt(0);
                        you.setGender(correctedGender);
                        System.out.println(you);
                        isCorrect = true;

                    }   
          }     
      } 

【问题讨论】:

  • Noob 最好在提出关于 SO 的问题之前先做一些研究
  • 有些人使用yoda conditions 来避免这个问题。 while(false == isCorrect)
  • 2 票?真的吗?这样的问题随处可见。
  • 哈!尤达条件。太棒了,以前从未听说过它。
  • 我想知道如果x = .. 是条件句中的受限产生式,那么这些错误中有多少永远不会存在 - 要么使其成为一个声明并完全避免这种产生式,要么要求将其表示为 @ 987654325@ 或类似号码。我想这就是 代码/样式检查器 的用途。我想对于字符串/对象相等性测试也可以这样说.. uhg.

标签: java boolean conditional


【解决方案1】:

在你的 while 循环中

while (isCorrect = false){

您使用的是赋值运算符=,所以它总是错误的。

如果值相同,您希望比较运算符 == 进行比较。

while (isCorrect == false){

由于它已经是boolean,您可能只想单独使用isCorrect

while (!isCorrect)

【讨论】:

  • ...并且可能使用:while (!isCorrect) 代替
  • @Mena - 是的,开始了!
  • 有道理。非常感谢
  • 我总是推荐后一种语法 (!isCorrect),因为它可以避免这个潜在的问题。
【解决方案2】:

您在 while 语句表达式中使用了赋值运算符,该运算符的计算结果始终为 false。请改用== 运算符来比较boolean

while (isCorrect == false){

或更好

while (!isCorrect){

【讨论】:

  • 更好:while(!isCorrect)
  • if(stillTyping == true) ??
【解决方案3】:

您可以通过这种方式在未来帮助自己:

 while( false == isCorrect )

如果你输入错误的相等运算符,这将引发编译错误。

【讨论】:

  • 我发现!isCorrect 在 Java 中做得更好。在像 C 这样的语言中,尤达条件更重要,因为 (x = 42) 的计算结果也为真值。
  • @user2246674 - 但它也适用于if(3 = x)
  • @MartinSmith 不在 Java 中 - if 要求输入表达式 boolean(x = 3) 的结果是 3,这是一个 int,因此 不兼容boolean - 这将是一个编译器类型错误。
  • @user2246674 啊不知道。谢谢!
猜你喜欢
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 2017-01-30
相关资源
最近更新 更多