平均字长

时间:2014-08-30 23:40:44

标签: java loops printing while-loop enter

我试图以非常简单的方式计算Java中用户输入的平均字长。我已经完成的代码的实际“数学”,它似乎工作得很好,但是为了完成代码,我需要解决一些奇怪的内容。

到目前为止,我有以下内容:

public class Main {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Please type some words, then press enter: ");

    int count = 0;
    double sum = 0;

    while (sc.hasNext()) {

        String userInput = sc.next();

        double charNum = userInput.length();
        sum = charNum + sum;
        count++;

        double average = 0;
        if (count > 0) {
            average = sum / count;
        }


        System.out.println("Average word length = " + average);

        }
    }
}

最终结果输出应如下所示:

run: 
Please type some words, then press enter: 
this is a test
Average word length = 2.75
BUILD SUCCESSFUL (total time: 10 seconds)

然而,输出看起来像这样:

run: 
Please type some words, then press enter: 
this is a test
Average word length = 4.0
Average word length = 3.0
Average word length = 2.3333333333333335
Average word length = 2.75

根据我编写的代码,如何更改它以便:

  • “平均字长”仅最后一次打印。
  • 程序在用户按下
  • 后结束

感谢您的任何建议。

4 个答案:

答案 0 :(得分:2)

您每次输入单词时都计算平均值,这不是您想要的。此外,即使按下输入,while循环也将继续。试试这个:

Scanner sc = new Scanner(System.in);

System.out.println("Please type some words, then press enter: ");

int count = 0;
double sum = 0;

String input = sc.nextLine();

String[] words = input.split("\\s+"); // split by whitespace

// iterate over each word and update the stats
for (String word : words) {
    double wordLength = word.length();
    sum += wordLength;
    count++;
}

// calculate the average at the end
double average = 0;
if (count > 0) {
    average = sum / count;
}

System.out.println("Average word length = " + average);

输出:

Please type some words, then press enter: 
this is a test
Average word length = 2.75

答案 1 :(得分:1)

您只需在循环后移动System.out.println并将average声明为循环以防止范围问题。但是,这样做更优雅:

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please type some words, then press enter: ");

        double average = 0;

        for (String word : sc.nextLine().split("\\s+")) 
            average += word.length();
        average /= words.length;
        System.out.println("Average word length = " + average);
        sc.close();
    }
}

sc.nextLine()返回用户输入的整行(没有最后一个“\ n”字符),split("\\s+")使用正则表达式\s+拆分此行,返回包含单词的数组。这个正则表达式意味着分割任何非空的空白字符序列。

答案 2 :(得分:0)

A)当所有平均值都已完成时,将输出打印到循环外

B)使用Date.getTime()在用户输入句子后获取程序开头的时间,并将其存储在变量中,然后在程序结束时,再次获取时间,减去从旧时代开始,除以1000将其从毫秒转换为秒。之后,您可以将其打印出来,但需要格式化

C)输出所有内容后调用sc.readLine(),这样当他们按下回车键时,该行将停止阻止并让程序结束。

答案 3 :(得分:0)

从while循环中获取System.out.println(...)。并在循环体之前声明average变量。


public class Main {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Please type some words, then press enter: ");

    String words = sc.nextLine();

    int count = 0;
    double sum = 0;
    double average = 0;

    sc = new Scanner(words);

    while (sc.hasNext()) {

        String userInput = sc.next();

        double charNum = userInput.length();
        sum = charNum + sum;
        count++;

        if (count > 0) {
            average = sum / count;
        }
    }
    System.out.println("Average word length = " + average);
}