繁体   English   中英

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

[英]Java: Do-while loop 'yes or no' response

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循环,所以这对我来说有点困难。 我也不用布尔值。

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

所以只需替换这个

letter = (char) input.read();

用这样的东西:

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

实际上,调用readLine()将使阅读器读取整行(包括回车符),并且仅返回与该输入字符对应的行尾之前的内容。

@Ole VV是正确的。 在循环结束时,正在读取用户在首次输入后输入的回车符。 下面的代码有效。 此外,还需要在循环中初始化计数器。

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');

    }
}

您可以改用Scanner,以下代码按预期等待输入。 另外,您可能想为新句子重置计数器:

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();

暂无
暂无

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

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