计算字符

Tom*_*myD 0 java character counting while-loop switch-statement

我在编写代码时遇到问题,只要用户没有结束"退出",就会生成一个while循环请求用户输入.当我运行代码时,它只生成第一个提示问题"输入一个句子或一个短语:",然后在输入短语后它不会计算while语句.有谁知道我做错了什么?

问题是:"让程序让用户继续输入短语而不是每次都重新启动它会很好.为此,我们需要围绕当前代码的另一个循环.也就是说,当前循环将嵌套在里面新的循环.添加一个外部while循环,只要用户没有输入短语quit就会继续执行.修改提示以告诉用户输入短语或退出退出.注意所有的初始化count应该在while循环中(也就是说我们希望用户输入的每个新短语重新开始计数).你需要做的就是添加while语句(并考虑读取的位置以便循环正常工作)确保完成程序并在添加代码后正确缩进 - 使用嵌套循环时,内循环应缩进."

    import java.util.Scanner;
    public class Count

    {
         public static void main (String[] args)
         {

              String phrase;    // A string of characters
              int countBlank;   // the number of blanks (spaces) in the phrase 
              int length;       // the length of the phrase
              char ch;      // an individual character in the string      

    int countA, countE, countS, countT; // variables for counting the number of each letter

    scan = new Scanner(System.in);

    // Print a program header
    System.out.println ();
    System.out.println ("Character Counter");
    System.out.println ("(to quit the program, enter 'quit' when prompted for a phrase)");
    System.out.println ();

    // Creates a while loop to continue to run the program until the user
    // terminates it by entering 'quit' into the string prompt.

        System.out.print ("Enter a sentence or phrase: ");
        phrase = scan.nextLine();

        while (phrase != "quit");
        {   
            length = phrase.length();



                // Initialize counts
                countBlank = 0;
                countA = 0;
                countE = 0;
                countS = 0;
                countT = 0;

                // a for loop to go through the string character by character
                // and count the blank spaces

                for (int i = 0; i < length ; i++)
                {
                    ch = phrase.charAt(i);

                    switch (ch)
                    {
                        case 'a':             // Puts 'a' in for variable ch
                        case 'A':  countA++;  // Puts 'A' in for variable ch and increments countA by 1 each time
                                    break;    // one of the variables exists in the phrase

                        case 'e':             // puts 'e' in for variable ch
                        case 'E':  countE++;  // ... etc.
                                    break;
                        case 's':
                        case 'S':  countS++;
                                    break;
                        case 't':
                        case 'T':  countT++;
                                    break;

                        case ' ':  countBlank++;
                                    break;
                    }

                }

            // Print the results
            System.out.println ();
            System.out.println ("Number of blank spaces: " + countBlank);
            System.out.println ();
            System.out.println ("Number of a's: " + countA);
            System.out.println ("Number of e's: " + countE);
            System.out.println ("Number of s's: " + countS);
            System.out.println ("Number of t's: " + countT);
            System.out.println ();



    }   
}
Run Code Online (Sandbox Code Playgroud)

}

Jon*_*eet 5

这是两个方面的问题:

while (phrase != "quit");
Run Code Online (Sandbox Code Playgroud)

首先,不要使用==!=进行字符串相等性检查.它仅检查参考标识.

其次,;最后意味着它将永远循环.它相当于:

while (phrase != "quit")
{
}
Run Code Online (Sandbox Code Playgroud)

你要:

while (!phrase.equals("quit"))
Run Code Online (Sandbox Code Playgroud)

想在循环结束,要求更多的投入.(仍然在循环体内,但在输出部分之后.)或者将循环条件更改为永久循环,在开始时请求输入,然后在输入"退出"时中断.