Java:Do-while循环'是或否'响应

时间:2016-08-13 10:52:37

标签: java

import java.io.*;

public class Page117
{

    public static void main(String[] args) throws IOException
    {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        char letter;
        String sentence;
        int counter = 0;
        char response = 'o';

        do
        {
            System.out.print("Type a sentence: ");
            sentence = input.readLine();
            System.out.print("Type a character to find: ");
            letter = (char) input.read();

            for(int x=0;x<sentence.length();x++)
            {
                if(letter==sentence.charAt(x))
                {
                    counter++;
                }
            }

            System.out.println("Number of times " + letter + " occured is: " + counter);
            System.out.print("Continue? [y/n]: ");
            response = (char) input.read();

        }while(response != 'n');          
    }        
}

关于程序:用户将输入一个句子。用户还将输入一个字符并计算该句子中出现的字符数。

我遇到了一个小问题。我如何做到这一点,以便在过程之后,它允许我输入我的回复。因为在我输入角色并告诉我出现次数之后,它会退出或赢得允许我输入任何内容。

我在代码中尝试了几乎所有内容。我没有使用do-while循环,所以这对我来说很难。我也没有那么多使用布尔值。

3 个答案:

答案 0 :(得分:2)

您面临的问题与您在提供多个字符时读取一个字符的事实有关:输入字符+回车符。

所以简单地替换这个

letter = (char) input.read();

有这样的事情:

letter = input.readLine().charAt(0);

确实调用readLine()会让读者读取整行(包含回车符),并且只返回与输入字符对应的行尾之前的内容。

答案 1 :(得分:2)

@Ole V.V是对的。在循环结束时读取用户在第一次输入后输入的回车。以下代码有效。还需要在循环内初始化计数器。

public class Page117 {

    public static void main(String[] args) throws IOException
    {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        char letter;
        String sentence;

        do
        {
            int counter = 0;
            char response = 'o';
            System.out.print("Type a sentence: ");
            sentence = input.readLine();
            System.out.print("Type a character to find: ");
            letter = input.readLine().charAt(0);

            for(int x=0;x<sentence.length();x++)
            {
                if(letter==sentence.charAt(x))
                {
                    counter++;
                }
            }

            System.out.println("Number of times " + letter + " occured is: " + counter);
            System.out.print("Continue? [y/n]: ");

        }while(input.readLine().charAt(0) != 'n');

    }
}

答案 2 :(得分:1)

您可以使用扫描仪,以下代码按预期等待输入。您可能还想重置一个新句子的计数器:

Scanner input = new Scanner(System.in);

char letter;
String sentence;
int counter = 0;
char response = 'o';

do
{
    counter = 0; // reset the counter for a new sentence

    System.out.print("Type a sentence: ");
    sentence = input.nextLine();
    System.out.print("Type a character to find: ");
    letter = (char) input.nextLine().charAt(0);

    for(int x=0;x<sentence.length();x++)
    {
        if(letter==sentence.charAt(x))
        {
            counter++;
        }
    }
    System.out.println("Number of times " + letter + " occured is: " + counter);
    System.out.print("Continue? [y/n]: ");
    response = (char) input.nextLine().charAt(0);

}while(response != 'n');  

input.close();