【问题标题】:Why is this causing a constant 'true' in this while loop?为什么这会在这个 while 循环中导致一个恒定的“真”?
【发布时间】:2018-01-08 15:08:34
【问题描述】:

这里是代码;

public class Main {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Do you need instructions for this game? Y/N.");
    char a = input.next().charAt(0);
    // This while loop always comes out as true.
    while (a != 'y' || a != 'n') {
        System.out.println("Please enter either Y/N. ");
        System.exit(0);
    }
    if (a == 'y') {
        System.out.println("This game is called heads and tails.");
        System.out.println("You must type h (heads), or t (tails).");
        System.out.println("You will need to keep guessing correctly, each correct guess will increase your score.");
    }
}
}

有没有解释为什么它总是被证明是真的,有没有其他方法可以做到这一点?我想要进行验证检查,如果用户输入的不是 y 或 n,程序就会关闭。

问题是,当我输入字符 y 或 n 时,即使我使用 !=(不等于)运算符,它仍然会关闭。

【问题讨论】:

  • 使用&& 而不是||
  • 因为如果a是'y',它肯定不是'n',反之亦然。
  • 你需要和&& 不是或||,y 不是n(反之亦然)。

标签: java while-loop constants


【解决方案1】:

如果你有a==y,那么a != 'n' 为真,a != 'y' || a != 'n' 为真。 如果你有a==n,那么a != 'y' 为真,a != 'y' || a != 'n' 为真。 如果你有a == other thinga != 'y' || a != 'n' 是真的。

OR 操作每次都是真的。需要使用 AND。

【讨论】:

    【解决方案2】:

    (a != 'y' || a != 'n') 至少有一个子条件必须为真。

    考虑三种可能的情况:

    a is 'y':            false || true  gives true
    a is 'n':            true  || false gives true
    a is something else: true  || true  gives true
    

    【讨论】:

      【解决方案3】:

      字符a不能同时是yn,所以while循环对于任何输入都会执行。

      此外,循环没有循环。

      【讨论】:

        【解决方案4】:

        您正在检查 a 是否不等于“y”或 a 是否不等于“n”。
        这总是正确的。

        把它改成while ((a != 'y') && (a != 'n'))

        【讨论】:

          【解决方案5】:

          while 中的条件 while (a != 'y' || a != 'n') 总是正确的,因为

          • 如果a等于y,那么a显然不等于n。所以,结果是真的。

          • 同样,如果a 等于n,那么a 显然不等于y。所以,结果是真的。

          • 同样,如果a 不等于yn,那么结果也为真。

          因此,while 中的条件始终为真。出于这个原因,执行进入while 循环并在打印您的消息后退出。

          所以使用AND 而不是OR 可能会解决您的问题,比如

          while(a != 'y' && a !='n') {
              //your work
          }
          

          我认为你愿意像下面那样做,

          public class Main { 
              public static void main(String[] args) {
                  Scanner input = new Scanner(System.in);
                  System.out.println("Do you need instructions for this game? Y/N: ");
                  char a = input.next().charAt(0); 
                  while (a != 'y') {
                      if(a =='n') {
                          System.exit(0);
                      }
                      else{
                           System.out.println("Please enter either Y/N : ");
                           a = input.next().charAt(0);
                      }
                  }
          
                  if (a == 'y') {
                      System.out.println("This game is called heads and tails.");
                      System.out.println("You must type h (heads), or t (tails).");
                      System.out.println("You will need to keep guessing correctly, each correct guess will increase your score.");
                  }
              } 
          }
          

          【讨论】:

            【解决方案6】:

            你的逻辑应该是“a is not y and a is not n”

            while (a != 'y' && a != 'n')

            【讨论】:

            • 有什么解释吗?
            • 虽然代码只回答有效,但请仍然尝试解释为什么必须使用这个答案以及为什么这个答案可以解决 OPs 问题
            猜你喜欢
            • 1970-01-01
            • 2015-01-17
            • 2014-05-06
            • 1970-01-01
            • 2012-10-11
            • 1970-01-01
            • 2012-09-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多