这是一个计算平均成绩的程序,我无法弄清楚我的代码是什么错误:
/**
* 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。
答案 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)
几个问题:
程序将如下: