用户字符串输入。计算字符

时间:2016-11-11 16:58:54

标签: java junit

我的代码工作,但我必须让它通过一些Junit测试。除了一个,它通过了所有。当角色进入任何东西时进入,进入大写,小写或两者的混合,当进入Hello World时它会起作用!

public static void main(String[] args)  {
    Scanner input = new Scanner(System.in);
    int[] textArray = new int[26];

    System.out.print("Enter text: ");
    readText(input, textArray);
}

public static void readText(Scanner input, int[]text){
    char letter = 0;

    if (input.hasNext() == false) {
        System.out.println();
    }
    else {
        while (input.hasNext()) {
            String a = input.nextLine();

            for (int i = 0; i < a.length(); i++) {
                letter = a.charAt(i);

                if (letter >= 'A' && letter <= 'Z') {
                    text[letter-65] = (text[letter-65]) + 1;
                }
                else if (letter >= 'a' && letter <= 'z') {
                    text[(letter - 32) - 65] = (text[(letter - 32) - 65]) + 1;
                }
                else if (letter == ' ') {
                    System.out.print("");
                }
                else {
                    System.out.print("");
                }
            }
        }
    }

    String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

    for (int y = 0; y < text.length; y++) {
        if (text[y] > 0) {
            System.out.println(alphabet[y] + ": " + text[y]);
        }
    }
}

Junit测试此输入:1 2 3%n! ? &gt;%n :) !!%n

预期输出

Enter text: 
    //empty line here

但我的代码的输出是

Enter text: //with no line after

我不确定如何在不破坏我的其他junit测试的情况下获得额外的线路。我尝试了一种方式,但它工作但我的Hello World无法正常工作。

其工作时的例子:
当我点击运行时,控制台会说

输入文字:

我有一个扫描仪输入,因此用户将输入一些单词,它将在控制台中显示为

输入文字:sOme wordS

然后它将计算每个字母的使用次数,并将其打印到控制台,如此

输入文字:sOme wordS
D:1
E:1
M:1
O:2
R:1
S:2
W:1

如果我在询问时没有输入任何内容,只需点击回车键即可输出

输入文字:
//这里空行

但是当我进入

输入文字:1 2 3
? ! &GT;
:) !!

输出最后不会添加额外的行。

1 个答案:

答案 0 :(得分:1)

尝试使用System.out.println("Enter text: ");代替System.out.print("Enter text: ");

更新:在澄清您的要求后,我建议使用以下代码。

public static void readText(Scanner input, int[]text){
        char letter = 0;

        while (input.hasNext()) {
            String a = input.nextLine();

            for (int i = 0; i < a.length(); i++) {
                letter = a.charAt(i);

                if (letter >= 'A' && letter <= 'Z') {
                    text[letter-65] = (text[letter-65]) + 1;
                }
                else if (letter >= 'a' && letter <= 'z') {
                    text[(letter - 32) - 65] = (text[(letter - 32) - 65]) + 1;
                }
                else if (letter == ' ') {
                    System.out.print("");
                }
                else {
                    System.out.print("");
                }
            }
        }

        String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

        boolean emptyOutput = true;
        for (int y = 0; y < text.length; y++) {
            if (text[y] > 0) {
                System.out.println(alphabet[y] + ": " + text[y]);
                emptyOutput = false;
            }
        }
        if (emptyOutput) {
            System.out.println();
        }
    }