简体   繁体   English

第一次用户输入后程序不会循环

[英]program won't loop after first user input

I have been trying to write this code where a user enters two words with or without a comma and if it does not have a comma print the an error saying so and then loop back to ask for another set of words.我一直在尝试编写此代码,其中用户输入两个带或不带逗号的单词,如果它没有逗号,则打印错误说明,然后循环返回以要求输入另一组单词。 Yes this is homework and I have searched the internet for help and it just has not clicked with me so far.是的,这是作业,我已经在互联网上搜索过帮助,但到目前为止还没有点击。 I am needing help with the loop in my code which is java.我的代码中的循环需要帮助,即 java。 These are the set of requirements for my warm up program followed by my code.这些是我的热身程序的一组要求,然后是我的代码。 Thank you for any help anyone can give.感谢您提供任何人可以提供的帮助。

1) Prompt the user for a string that contains two strings separated by a comma. 1) 提示用户输入包含两个用逗号分隔的字符串的字符串。 2) Report an error if the input string does not contain a comma. 2) 如果输入字符串不包含逗号,则报告错误。 3) Extract the two words from the input string and remove any spaces. 3) 从输入字符串中提取两个单词并删除所有空格。 Store the strings in two separate variables and output the strings.将字符串存储在两个单独的变量中并输出字符串。 4) Using a loop, extend the program to handle multiple lines of input. 4) 使用循环,扩展程序以处理多行输入。 Continue until the user enters q to quit.继续直到用户输入 q 退出。 Here is my code:这是我的代码:

import java.util.Scanner;
public class ParseStrings {

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);
        Scanner inSS = null;
        String lineString = "";
        String firstWord = "";
        String secondWord = "";
        boolean inputDone = false;

        System.out.println("Enter input string: ");

        while (!inputDone) {
            lineString = scnr.nextLine();
            inSS = new Scanner(lineString);

            if (firstWord.equals("q")) {
                System.out.println("Exiting.");
                inputDone = true;

            }

            if (lineString.contains(",")) {
                String[] parts = lineString.trim().split("\\s*,\\s*");
                firstWord = parts[0];
                secondWord = parts[1];
                System.out.println("First word: " + firstWord);
                System.out.println("Second word: " + secondWord);
            } else {
                System.out.println("Error: No comma in string");

            }
            break;
        }
        return;
    }
}

1) You do not need the return statement for a main method of type void 1) 对于void类型的 main 方法,您不需要return语句

2) The break at the end of your loop is what is terminating it after the first run. 2)循环结束时的break是第一次运行后终止它的原因。

Writing break;写作break; within your loop is the same thing as telling your loop to stop looping.在循环中与告诉循环停止循环相同。 If you want to define another condition to terminate your loop, but dont want to put it in your while , then put your break inside of some sort of condition, that way it doesn't happen every single time.如果你想定义另一个条件来终止你的循环,但又不想把它放在你的while ,那么把你的 break 放在某种条件中,这样它就不会每次都发生。

I am trying to figure out when the user enters q the program quits here is my code so far.我试图弄清楚用户何时输入 q 程序退出这里是我的代码到目前为止。

import java.util.Scanner;导入 java.util.Scanner;

public class ParseStrings {公共类 ParseStrings {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    while (true) {
        String s = sc.nextLine();
        System.out.println("Enter input string: ");
        if (s.indexOf(",") == -1) //checks if there is a comma in the string
        {
            System.out.println("Error: No comma in string");
        } else {
            //there is a comma in the string
            String s1 = s.substring(0, s.indexOf(","));
            String s2 = s.substring(s.indexOf(",") + 1);
            s1 = s1.replace(" ", "");
            s2 = s2.replace(" ", "");
            //store both the strings in s1 and s2
            System.out.println("First word: " + s1);

            System.out.println("Second word: " + s2);

            s1 = s1.trim();
            s2 = s2.trim();  //remove extra spaces

            System.out.println(s1 + " " + s2);
            break;
        }
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM