【问题标题】:Finding the percentage in my while loop isn't working correctly. Is nested while loop ok?在我的 while 循环中查找百分比无法正常工作。嵌套while循环好吗?
【发布时间】:2015-07-14 17:55:35
【问题描述】:

我正在尝试学习 java while 循环。我正在尝试制作一个程序,该程序将计算一组学生的通过考试成绩,并输出输入的总成绩、分数高于 69 的通过考试的数量,并显示通过的考试百分比。

我遇到的问题是我似乎无法正确输出百分比。它一直显示0.0。以下是迄今为止我想出的最好的代码。

嵌套的while循环是一种好的编码风格吗?有没有更简单的方法来缩短我的程序?谢谢

    import java.util.Scanner;
    import java.text.DecimalFormat;

    public class CountPassingScores {

     public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        // Formats the percentage output to one decimal place.
        DecimalFormat df = new DecimalFormat("###,##0.0");

        // counts how many times a score is entered. Passing score is not
        // considered here.
        int count = 0;
        // The score the user enters.
        int score = 0;
        // percent of the class that passed the test. Passing score is 70 and
        // above.
        double percentOfClassPassed = 0.0;
        // total number of tests passed. Passing score is 70 and above.
        int numberOfTestsPassed = 0;

        System.out.println("This program counts the number of passing "
                + "test scores. (-1 to quit)\n");

        while (score != -1) {
            System.out.print("Enter the first test score:  ");
            score = scan.nextInt();

            while (count != -1 && score > 0) {
                System.out.print("Enter the next test score:  ");
                score = scan.nextInt();
                count++;

                if (count == -1)
                    break;
                else if (score > 69)
                    numberOfTestsPassed++;
                percentOfClassPassed = (numberOfTestsPassed / count);
            }

        }

        System.out.println("\nYou entered " + count + " scores.");
        System.out.println("The number of passing test scores is "
                + numberOfTestsPassed + ".");
        System.out.println(df.format(percentOfClassPassed)
                + "% of the class passed the test.");
    }
}

【问题讨论】:

  • 你听说过整数除法吗?如果不尝试谷歌它,你会明白你的问题。

标签: java if-statement while-loop java.util.scanner decimalformat


【解决方案1】:

您的代码不会将 92 视为测试通过分数,因为您没有在第一个 while 循环中增加 numberOfTestsPassed 的值。以下是我在您的代码 sn-p 中所做的一些更改:

    while (score != -1) {
        System.out.print("Enter the first test score:  ");
        score = scan.nextInt();
        if(score > 69)
        numberOfTestsPassed++;
        while (count != -1 && score > 0) {
            System.out.print("Enter the next test score:  ");
            score = scan.nextInt();
            count++;
            if (score == -1)
                break;
            else if (score > 69)
                numberOfTestsPassed++;
        }
         percentOfClassPassed = ((double)numberOfTestsPassed * 100 / count); 
    }

它为所有输入提供正确的输出。

【讨论】:

  • 非常感谢。我让它正常工作。我没想到要添加额外的 if。为什么 numberOfTestsPassed 没有增加两次,因为有两个?而且我不确定如何修复数学部分。谢谢
【解决方案2】:

这是因为您将intint 分开。这只会导致int

要获得正确的结果,请将任何一个转换为 double

percentOfClassPassed = ((double) numberOfTestsPassed / count);

【讨论】:

  • 啊,我明白了,我还没有学到很多关于类型转换的知识。谢谢。
  • 无论我输入哪个分数,我似乎都得到了相同的输出。 0.5%。
  • 有谁知道我添加后的原因。 percentOfClassPassed = ((double) numberOfTestsPassed / count);它现在输出 0.5%?
  • 你能再帮我一次吗
  • 它给了我正确的答案。您的代码的一个问题是,即使我让-1 退出,您也在计算百分比。而不是if (count == -1) 使用if (score == -1)。此外,由于您四舍五入到小数点后一位,您只能得到近似值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 2015-01-06
  • 1970-01-01
相关资源
最近更新 更多