【问题标题】:How do I create a while loop around a for loop that will allow the user to quit the loop?如何围绕允许用户退出循环的 for 循环创建一个 while 循环?
【发布时间】:2016-10-20 02:18:04
【问题描述】:

我是循环的新手,我已经编写了一个 for 循环,提示用户输入一个短语,for 循环打印出有多少空格,a's,e's ,s's 和 t's。我需要做一个while循环,允许用户输入“quit”来退出循环。我也是学习开关打印字母的新手。我已经尝试使用我的代码来修复我的错误(持续循环或没有结果)超过一周。下面是我的代码,在此先感谢您。

import java.util.Scanner;
public  class Count
{

public static void main (String[] args)
{

    Scanner scan = new Scanner (System.in);
    String phrase;    // a string of characters
    int countBlank;   // the number of blanks (spaces) in the phrase 
    int i = 0;
    char ch;          // an individual character in the string
    String quit = "quit";
    System.out.println ();
    System.out.println ("Character Counter");
    System.out.println ();

    System.out.print ("Enter a sentence or phrase (quit to quit): ");
    phrase = scan.nextLine(); // read input Scanner  
    int length = phrase.length();       // the length of the phrase

    countBlank = 0;
    int countA = 0;
    int countE = 0;
    int countS = 0;
    int countT = 0;

    //I am not sure how to set up the start of the while loop
    //while (!phrase.equals("quit")){
    //while (phrase != "quit"){

    for (i = 0; i < length; i++){
        ch = phrase.charAt(i);
        if (ch == (' '))
            countBlank++;
        switch (ch)
        {
        case 'a': case 'A':  countA++; break;
        case 'e': case 'E':  countE++; break;
        case 's': case 'S':  countS++; break;
        case 't': case 'T':  countT++; break;       
        }
    }
    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);
    }
}

输出:

Character Counter

Enter a sentence or phrase (quit to quit): AEST aest

Number of blank spaces: 1

Number of a's: 2
Number of e's: 2
Number of s's: 2
Number of t's: 2

【问题讨论】:

    标签: java loops while-loop switch-statement


    【解决方案1】:

    您在正确的轨道上,但您确实需要在该过程完成后再次阅读该短语以使 while 循环正常工作。否则,将是无限循环。 以下是我所做的一些更改:

     while (!phrase.equals("quit")){
    
            for (i = 0; i < length; i++){
                ch = phrase.charAt(i);
                if (ch == (' '))
                    countBlank++;
                switch (ch)
                {
                case 'a': case 'A':  countA++; break;
                case 'e': case 'E':  countE++; break;
                case 's': case 'S':  countS++; break;
                case 't': case 'T':  countT++; break;       
                }
            }
            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);
            phrase =scan.nextLine(); //read next phrase from user to continue.
            }
    

    【讨论】:

    • 哇,这真的很简单。我实际上尝试过使用它,但我把它放在了错误的位置。谢谢
    • 我尝试运行我的代码,但是一旦我输入了一个短语并打印了结果,当我输入另一个短语时,它并没有打印出任何结果
    • 工作正常!我测试了它。你复制粘贴了我的代码吗?
    • 我尝试复制和粘贴您的代码,但我收到一条错误消息“java.lang.StringIndexOutOfBoundsException: String index out of range: 4”
    • 它正在谈论 ch = phrase.charAt(i);
    【解决方案2】:

    phrase = scan.nextLine(); 之后使用它

    if(phrase.equalsIgnoreCase("quit")){ return; }

    【讨论】:

    • 是的,我们可以在switch语句中使用break。但我相信早期存在可以避免for循环的冗余执行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2021-07-27
    • 2015-01-18
    • 2014-08-21
    • 2013-03-01
    相关资源
    最近更新 更多