字符串

时间:2017-05-27 23:05:38

标签: java string loops

我正在尝试编写一个程序,允许输入各种单词(一步一步),直到一个类型“退出”。 我在停止循环时遇到问题(即使输入了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);

    }

}}

5 个答案:

答案 0 :(得分:0)

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

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

有关详细信息,请参阅Object.equals()

答案 1 :(得分:0)

正如在另一个答案中所指出的,您需要使用.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);
    }
}

答案 2 :(得分:0)

使用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);

}

我希望这能帮到你,祝你好运。

答案 3 :(得分:0)

您应该使用equals来比较字符串。在这种情况下,您最好使用while代替for来循环。

当您使用equalsIgnoreCase时,无论该单词是大写还是小写(相同),都表示无效。

当用户输入“退出或结束或退出或结束”

时,应用程序即告完成

示例:

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

答案 4 :(得分:-3)

在System.out.println之后简单地执行System.exit(0)(&#34;结束系统&#34;);