为什么在满足条件时while循环不会停止?

时间:2020-01-11 04:07:22

标签: java if-statement while-loop conditional-statements computer-science

**我正在尝试解决这个问题:

允许用户使用标准输入来提出投诉,直到他们输入短语“仅此而已”。如果他们说他们“讨厌”某物,则说您讨厌同一物品。如果他们说某人“做了”某事,请回答“ OH NO name” 没有!?!?”。请记住,“我”应转换为“您”。如果他们说“我不知道 词组1或词组2”的问题更糟,回答一个问题比另一个问题差,但两者都很漂亮 坏。随机选择,结果会更糟。**

我写了下面的代码,但是由于某些奇怪的原因,我输入的任何内容都没有得到处理。根据该问题,除此以外的所有内容均应停止该程序,但即使输入“仅此而已”,该程序仍继续运行。此外,每当我输入“我讨厌某事”或上述问题中的任何条件时,它都不会打印出任何内容。您能告诉我我的代码有什么问题吗?在此先谢谢您。:)

  public static void airingOfGrievances(){
    Scanner input = new Scanner(System.in);
    String userInput = input.next() + input.nextLine();

    while (userInput != "that is all")
    {
     String[] split = userInput.split(" ");
     String[] split2 = userInput.split(",");
     if (userInput == "testing")
     {
      System.out.println("working");    
     }
     for (int i = 0; i < split.length; i++)
     {
      if (split[i] == "hate")
      {
       System.out.println ("I also hate " + split[split.length - 1]);  
       break;
      }
      else if (split[i] == "did")
      {
       System.out.println ("OH NO " + split[0] + " didn't!?!?");
       break;
      }
      else if (split[0] == "I don’t know what is worse,")
      {
       String[] split3 = split2[1].split(" ");
       double random = Math.random();
       if (random >= 0.5)
       {
        System.out.println(split3[2] + " are worse but both are pretty bad");    
       }
       else if (random <= 0.5)
       {
        System.out.println(split3[0] + " are worse but both are pretty bad");    
       }
       break;
      }
     }
     userInput = input.next() + input.nextLine();
     System.out.println(userInput);
    }
   }

0 个答案:

没有答案