【问题标题】:Java basic usage of string [duplicate]字符串的Java基本用法[重复]
【发布时间】:2017-05-27 23:05:38
【问题描述】:

我正在尝试编写一个程序,该程序允许输入各种单词(逐步),直到输入 "quit" 为止。 我无法停止循环(即使输入了退出这个词,它也不会停止) 使用System.out.println(sum); 我可以检查单词是否加起来,但它永远不会停止.. ((Summary : if(string == "quit") 不起作用,(String end = "quit"; string!=end;) 不起作用)) 抱歉,如果它难以阅读。这是我的第二天编码:(

public static void main(String[] args) {
    java.util.Scanner scanner = new java.util.Scanner(System.in);
    String sum = "";
    System.out.println("Type in a word");
    String string = scanner.nextLine();
    if (string == "quit")
    {System.out.println("Ending system");

    }
    else{
    for(String end = "quit"; string!=end;  )
    {
        sum = sum + " " + string;
        System.out.println(sum);
        System.out.println("Type in another word");
        String stringextra = scanner.nextLine();
        if(stringextra == "quit"){break;}
        string = stringextra;

    }

    scanner.close();
    System.out.println("Stopping... due to the word quit");
    System.out.println("all the words typed are " + sum);

    }

}}

【问题讨论】:

  • 简单使用:if(stringextra.equals("quit")){break;}

标签: java string loops


【解决方案1】:

字符串 string"quit" 不是相同的对象(存储在 jvm 中)但相等,因此使用 == 进行检查将不起作用。

您需要使用: string.equals("quit")

更多信息请参见Object.equals()

【讨论】:

    【解决方案2】:

    正如在另一个答案中指出的那样,您需要使用 .equals() 来比较字符串。此外,您的 for 循环不正确。在这种情况下,通常使用 while 循环:

    public class end_on_quit {
    
       public static void main(String[] args) 
       {
          java.util.Scanner scanner = new java.util.Scanner(System.in);
          String sum = "";
          System.out.println("Type in a word");
          String string = scanner.nextLine();
          while ( ! string.equals("quit") )
          {
             sum = sum + " " + string;
             System.out.println(sum);
             System.out.println("Type in another word");
             string = scanner.nextLine();
           }
           System.out.println("Ending system");
           scanner.close();
           System.out.println("Stopping... due to the word quit");
           System.out.println("all the words typed are " + sum);
        }
    }
    

    【讨论】:

      【解决方案3】:

      使用do while 执行代码之类的最简单方法,这样您的代码就会一直运行到用户写入quit。我给你举个简单的例子:

      public static void main(String[] args) {
      
          Scanner scanner = new Scanner(System.in);
          String sum = "";
          System.out.println("Type in a word");
          String stringextra = scanner.nextLine();
      
          if (stringextra.equalsIgnoreCase("quit")) {
              System.out.println("Bye bye");
              System.exit(0);
          } else {
              do {
                  sum = sum + " " + stringextra;
                  System.out.println(sum);
                  System.out.println("Type in another word");
                  stringextra = scanner.nextLine();
      
              } while (!stringextra.equalsIgnoreCase("quit"));
      
          }
          scanner.close();
          System.out.println("Stopping... due to the word quit");
          System.out.println("all the words typed are " + sum);
      
      }
      

      希望对你有帮助,祝你好运。

      【讨论】:

        【解决方案4】:

        你应该使用equals来比较字符串。在这种情况下,您的示例最好使用while 而不是for 循环。

        当你使用equalsIgnoreCase时,意思是不管单词是大写还是小写(都是一样的)。

        当用户输入“quit or ends or QUIT or ENDS”时应用程序结束

        示例:

        public static void main(String[] args) {
            java.util.Scanner scanner = new java.util.Scanner(System.in);
            String sum = "";
            System.out.println("Type in a word");
            String string = scanner.nextLine();
        
            while (!string.equalsIgnoreCase("quit")) {
                sum = sum + " " + string;
                System.out.println(sum);
                System.out.println("Type in another word");
                string = scanner.nextLine();
        
                if (string.equalsIgnoreCase("ends")) {
                    string = "quit";
                }
            }
        
            System.out.println("Ending system");
        
            scanner.close();
            System.out.println("Stopping... due to the word quit");
            System.out.println("all the words typed are " + sum);
            System.exit(0);
        }
        

        输出:

        Type in a word
        hi
        Type in another word
        this
        Type in another word
        an
        Type in another word
        example
        Type in another word
        ENDS
        Ending system
        Stopping... due to the word quit
        all the words typed are  hi this an example
        
        Process finished with exit code 0
        

        【讨论】:

          【解决方案5】:

          只需在 System.out.println("Ending system"); 之后执行 System.exit(0);

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-10-22
            • 2020-01-22
            • 2023-04-02
            • 2012-07-14
            • 2017-03-05
            • 2016-07-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多