Java 如何在循环中保持字符串输入

Java 如何在循环中保持字符串输入,java,string,loops,input,Java,String,Loops,Input,我正在制作一个程序,从用户那里获取一个字符串,并用分隔符将其逐字分隔,它几乎完成了。但是,在第一个完整循环之后,用户的下一个输入不会通过最后一个while循环 下面是我要说的代码片段: do { System.out.println ("\nEntered String: " + s1 + "\n"); while (input.hasNext()) { word++; System.out.println ("Word #

我正在制作一个程序,从用户那里获取一个字符串,并用分隔符将其逐字分隔,它几乎完成了。但是,在第一个完整循环之后,用户的下一个输入不会通过最后一个while循环

下面是我要说的代码片段:

do
  { 
     System.out.println ("\nEntered String: " + s1 + "\n");

     while (input.hasNext())
     {
        word++;

        System.out.println ("Word #" + word + ": \t" + input.next()); 
     }   
        System.out.print ("\nEnter 'q' to quit, enter string to continue:  \t");
        s1 = scan.nextLine();

  } while (!s1.equals("q"));
我在想,我需要在单词increment和print行周围进行另一个while循环,并在input.hasNext()循环中使用continue序列,因为我得到了一个使用int的类似程序,但我不确定如何处理字符串

有什么建议吗

编辑:为了澄清,现在我的代码的输出如下所示:

 while(input.hasNextLine()) {
    String line = input.nextLine();
    String[] words = line.split("delimeter");
    if(words.length < 1 || words[words.length - 1].equals("q")) {
        break;
    }
 }
输入一个句子:这是一个句子

输入字符串:这是一个句子

单词1:这个

单词#2:是

单词#3:a

单词#4:句子

输入'q'退出,输入string继续:另一句话

输入字符串:另一个句子

输入“q”退出,输入字符串继续:


我需要像“这是一个句子”这样的“另一个句子”打印出来。

如果行得通,你可以试试这个。它与您需要的结果相同

    Scanner scan = new Scanner(System.in);
    String s1 = scan.nextLine();
    do {
        String input[] = s1.split(" ");
        System.out.println ("\nEntered String: " + s1 + "\n");
        for(int i = 0; i < input.length; i++) {
           System.out.println ("Word #" + i+1 + ": \t" + input[i]); 
        }
        System.out.print ("\nEnter 'q' to quit, enter string to continue:  \t");
        s1 = scan.nextLine();
    } while (!s1.equals("q"));
Scanner scan=新的扫描仪(System.in);
字符串s1=scan.nextLine();
做{
字符串输入[]=s1.split(“”);
System.out.println(“\n输入字符串:“+s1+”\n”);
for(int i=0;i
如果有效,您可以尝试此方法。它与您需要的结果相同

    Scanner scan = new Scanner(System.in);
    String s1 = scan.nextLine();
    do {
        String input[] = s1.split(" ");
        System.out.println ("\nEntered String: " + s1 + "\n");
        for(int i = 0; i < input.length; i++) {
           System.out.println ("Word #" + i+1 + ": \t" + input[i]); 
        }
        System.out.print ("\nEnter 'q' to quit, enter string to continue:  \t");
        s1 = scan.nextLine();
    } while (!s1.equals("q"));
Scanner scan=新的扫描仪(System.in);
字符串s1=scan.nextLine();
做{
字符串输入[]=s1.split(“”);
System.out.println(“\n输入字符串:“+s1+”\n”);
for(int i=0;i
您正在使用

 while (input.hasNext())
我假设输入是一个Scanner对象,所以在使用它之前(但在进入循环之前)应该执行类似的操作:

您正在使用

 while (input.hasNext())
我假设输入是一个Scanner对象,所以在使用它之前(但在进入循环之前)应该执行类似的操作:


与其在
while
循环中使用
scanner.next()
,我建议您执行以下API:

 String.split
 Scanner.nextLine
大概是这样的:

 while(input.hasNextLine()) {
    String line = input.nextLine();
    String[] words = line.split("delimeter");
    if(words.length < 1 || words[words.length - 1].equals("q")) {
        break;
    }
 }
while(input.hasNextLine()){
String line=input.nextLine();
String[]words=line.split(“delimeter”);
if(words.length<1 | | words[words.length-1]。等于(“q”)){
打破
}
}

循环时,我建议使用以下API,而不是使用
scanner.next()

 String.split
 Scanner.nextLine
大概是这样的:

 while(input.hasNextLine()) {
    String line = input.nextLine();
    String[] words = line.split("delimeter");
    if(words.length < 1 || words[words.length - 1].equals("q")) {
        break;
    }
 }
while(input.hasNextLine()){
String line=input.nextLine();
String[]words=line.split(“delimeter”);
if(words.length<1 | | words[words.length-1]。等于(“q”)){
打破
}
}

我不太明白问题出在哪里,因为您的代码没有编译。但不需要另一个循环。下面是一些有效的代码:

Scanner scan = new Scanner(System.in);
System.out.print("\nEnter a sentence:");
String s1 = scan.nextLine();

do
{
    System.out.println("\nEntered String: " + s1 + "\n");

    Scanner input = new Scanner(s1);
    int word = 0;
    while (input.hasNext())
    {
        word++;
        System.out.println("Word #" + word + ": \t" + input.next());
    }

    System.out.print("\nEnter 'q' to quit, enter string to continue:  \t");
    s1 = scan.nextLine();

} while(!s1.equals("q"));

scan.close();

我不太明白到底是什么问题,因为你的代码没有编译。但不需要另一个循环。下面是一些有效的代码:

Scanner scan = new Scanner(System.in);
System.out.print("\nEnter a sentence:");
String s1 = scan.nextLine();

do
{
    System.out.println("\nEntered String: " + s1 + "\n");

    Scanner input = new Scanner(s1);
    int word = 0;
    while (input.hasNext())
    {
        word++;
        System.out.println("Word #" + word + ": \t" + input.next());
    }

    System.out.print("\nEnter 'q' to quit, enter string to continue:  \t");
    s1 = scan.nextLine();

} while(!s1.equals("q"));

scan.close();

input
的值可能重复?输入用于分隔符,即用户输入的字符串s1。它的工作方式与这里几乎相同:但我修改了代码,允许用户从扫描仪输入。
input
的值可能重复?输入用于分隔符,即用户输入的字符串s1。它的工作原理与这里几乎相同:但我修改了代码,允许用户从扫描仪输入。这就解决了它!我只需要将扫描仪输入放入do while循环中。谢谢这就解决了!我只需要将扫描仪输入放入do while循环中。谢谢