我的代码中的数学怎么了?

时间:2015-10-02 09:51:07

标签: java sum average addition

这是一个计算平均成绩的程序,我无法弄清楚我的代码是什么错误:

/**
 * This program will calculate grade average of user input
 * Programmer Tasdiq Chowdhury
 *  Date: 10/2/2015
 */
import java.util.Scanner;

public class GradeAVG {

    public static void main(String[] args) {
        avgGrade();
    }

    public static void avgGrade() {
        Scanner keyboard = new Scanner (System.in);
        double count = 0;
        double avgGrade = 0 ; 
        double grade;
        double total = 0; 
        System.out.println("Please input the grade");
        grade = keyboard.nextDouble();

        while(true){
            System.out.println("Please input the grade");           
            grade= keyboard.nextDouble();
            count = count + 1;
            if (grade < 0) break;

            total += grade;
            avgGrade = total/count;
        }

        System.out.println ("Sum is " +total);
        System.out.printf("The average of the  %.0f grades are %.2f " ,count ,avgGrade);
    }   

}

输出:

Please input the grade
100
Please input the grade
50
Please input the grade
-9
Sum is 50.0
The average of the  2 grades are 50.00 

总和应为150,平均为75。

4 个答案:

答案 0 :(得分:3)

问题是你在while循环开始之前正在读取用户的成绩,之后你忽略了这个值。

你应该删除这2行,事情将按预期工作。我在下面的代码段中对这些行进行了评论,以明确地向您显示问题。

<div id="not_to_pdf">Not to Pdf</div>
<div id="pdf">To Pdf</div> 

<form action="" method="post">
  <input name="content" id="content" type="hidden"/>
  <input type="submit" name="submit" value="PDF" onclick="document.getElementById('content').value=document.getElementById('not_to_pdf').innerHTML">
</form>

<?php if ($_POST['submit']) {
require_once("pdf/mpdf.php");
$mpdf=new mPDF('c','A4','','' , 0 , 0 , 0 , 0 , 0 , 0);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first 
$pdf = "value of div!";
$mpdf->WriteHTML($_POST['content']);


 $mpdf->Output('x.pdf', 'D'); 

} ?>

作为旁注,您应该始终尽量减少每个变量的范围。这里,public static void avgGrade() { Scanner keyboard = new Scanner(System.in); double count = 0; double avgGrade = 0; double grade; double total = 0; // System.out.println("Please input the grade"); // grade = keyboard.nextDouble(); while(true){ System.out.println("Please input the grade"); grade = keyboard.nextDouble(); count = count + 1; if (grade < 0) break; total += grade; avgGrade = total/count; } System.out.println ("Sum is " +total); System.out.printf("The average of the %.0f grades are %.2f " ,count ,avgGrade); } 变量仅在grade循环内使用,因此您可以直接编写while并删除方法开头的声明。

答案 1 :(得分:1)

您没有将first循环之外的while成绩添加到total。 如果count不可接受,则grade递增也没有意义,因此只有在increment检查后才count grade

您可以重写像

这样的while块
    while (true) {
        System.out.println("Please input the grade");
        grade = keyboard.nextDouble();
        if (grade < 0)
            break;

        count = count + 1;
        total += grade;


    }
    avgGrade = total / count;

    System.out.println("Sum is " + total);
    System.out.printf("The average of the  %.0f grades are %.2f ", count,
            avgGrade);

}

答案 2 :(得分:1)

谢谢你们。这是最终的代码

       /**
       * This program will calculate grade average of user input
       * 
       *    Date: 10/2/2015
       */
       import java.util.Scanner;
       public class GradeAVG {



public static void main(String[] args) {

    avgGrade()

}

public static void avgGrade() 
{
    Scanner keyboard = new Scanner (System.in);

    double count = 0;
    double avgGrade = 0 ; 
    double total = 0; 

    while(true){
            System.out.println("Please input the grade");           
         double grade= keyboard.nextDouble();

        if (grade < 0) break;
        count = count + 1;
         total += grade;

        avgGrade = total/count;



            }

        System.out.println ("Sum is " +total);
        System.out.printf("The average of the  %.0f grades are %.2f " ,count ,avgGrade);


}   

}

答案 3 :(得分:0)

几个问题:

  • 首先,在第一次读取之前,你会对成绩进行两次分配(第一次在while循环之前,第二次在while循环之内),所以第一次输入将被完全忽略
  • 其次,在检查是否要打破while循环之前递增count变量,因此最终计数值应高于1
  • 第三,平均值在循环内计算,不会在最后(部分)迭代中重新计算

程序将如下:

  • 输入:100,计数:0,总计:0,平均:0
    • 输入被忽略
  • 输入:50,计数:1,总数:50,平均值:50
  • 输入:-9,计数:2,总数:50,平均值:50
    • 循环退出,但增加计数之前;没有重新计算avg