该程序将通过提示使用for循环计算4次考试的平均成绩 考试成绩的用户,一次一个,然后计算平均值并显示结果。
public class ExamsFor4 {
public static void main(String[] arguments) {
int inputNumber; // One of the exams input by the user.
int sum = 0; // The sum of the exams.
int i; // Number of exams.
Double Avg; // The average of the exams.
TextIO.put("Please enter the first exam: "); // get the first exam.
inputNumber = TextIO.getlnInt();
for ( i = 1; i <= 4; i++ ) {
sum += inputNumber; // Add inputNumber to running sum.
TextIO.put("Please enter the next exam: "); // get the next exam.
inputNumber = TextIO.getlnInt();
if (i == 4) {
Avg = ((double)sum) / i;
TextIO.putln();
TextIO.putln("The total sum for all " + i +" exams is " + sum);
TextIO.putf("The average for the exams entered is %1.2f.\n", Avg);
break;
}
}
} // end main ()
} // end class ExamsFor4
我的结果:
Please enter the first exam: 100
Please enter the next exam: 99
Please enter the next exam: 98
Please enter the next exam: 97
Please enter the next exam: 96
The total sum for all 4 exams is 394
The average for the exams entered is 98.50.
这是正确的,除了最后一次打印:'Please enter the next exam: 96
'
我尝试将IF语句放在“sum
”行和TextIO.put 'Enter next exam'
之间,但是隔离了它。
谢谢,来自程序员世界的网络家伙陷阱。
答案 0 :(得分:12)
你有一个所谓的 off-by-one error ,而且你不必要地卷积你的循环逻辑。
关于循环,我建议两件事:
for (int i = 1; i <= N; i++)
;这是非典型的
for (int i = 0; i < N; i++)
;这是更典型的Double Avg
在Java中,变量名以小写开头。此外,Double
是引用类型,即原始double
的框。您应该尽可能选择double
到Double
int
and an Integer
in Java/C#? int num = Integer.getInteger(“123”)
throw NullPointerException
? boolean
? new Integer(i) == i
in Java?(是!!!)这是一种重写代码的方法,使代码更具可读性。我使用java.util.Scanner
,因为我不认为TextIO
是标准的,但实质上仍然是相同的。
import java.util.*;
public class ExamsFor4 {
public static void main(String[] arguments) {
Scanner sc = new Scanner(System.in);
final int NUM_EXAMS = 4;
int sum = 0;
for (int i = 0; i < NUM_EXAMS; i++) {
System.out.printf("Please enter the %s exam: ",
(i == 0) ? "first" : "next"
);
sum += sc.nextInt();
}
System.out.printf("Total is %d%n", sum);
System.out.printf("Average is %1.2f%n", ((double) sum) / NUM_EXAMS);
}
}
示例会话如下:
Please enter the first exam: 4
Please enter the next exam: 5
Please enter the next exam: 7
Please enter the next exam: 9
Total is 25
Average is 6.25
请注意:
final
有意义,请执行此操作
(360 / 24) / 60 = 0
in Java
(double)
之前的转换,以便它执行浮点除法。?:
运算符,也称为条件运算符。答案 1 :(得分:3)
将结束条件严格改为小于4,并将打印出的总代码和平均值放在循环外。
答案 2 :(得分:0)
你应该把if语句放在for循环之外。这样你就不需要if语句了。其次,循环中的语句应该是&lt; 4而不是&lt; = 4.
public class ExamsFor4 {
public static void main(String[] arguments) {
int inputNumber; // One of the exams input by the user.
int sum = 0; // The sum of the exams.
int i; // Number of exams.
Double Avg; // The average of the exams.
TextIO.put("Please enter the first exam: "); // get the first exam.
inputNumber = TextIO.getlnInt();
for ( i = 1; i < 4; i++ ) {
sum += inputNumber; // Add inputNumber to running sum.
TextIO.put("Please enter the next exam: "); // get the next exam.
inputNumber = TextIO.getlnInt();
}
Avg = ((double)sum) / i;
TextIO.putln();
TextIO.putln("The total sum for all " + i +" exams is " + sum);
TextIO.putf("The average for the exams entered is %1.2f.\n", Avg);
break;
} // end main ()
}
答案 3 :(得分:0)
只需对代码进行少量更改即可。但是你应该遵循一些答案中提出的更清洁的方法。
public class ExamsFor4 {
public static void main(String[] arguments) {
int inputNumber; // One of the exams input by the user.
int sum = 0; // The sum of the exams.
int i; // Number of exams.
double Avg; // The average of the exams.
TextIO.put("Please enter the first exam: "); // get the first exam.
inputNumber = TextIO.getlnInt();
sum += inputNumber;
for ( i = 1; i < 4; i++ ) {
TextIO.put("Please enter the next exam: "); // get the next exam.
inputNumber = TextIO.getlnInt();
sum += inputNumber; // Add inputNumber to running sum.
}
Avg = ((double)sum) / i;
TextIO.putln();
TextIO.putln("The total sum for all " + i +" exams is " + sum);
TextIO.putf("The average for the exams entered is %1.2f.\n", Avg);
} // end main ()
} // end class ExamsFor4
答案 4 :(得分:0)
import java.util.Scanner;
public class ExamsFor4 {
public static void main(String[] arguments) {
int sum = 0; // The sum of the exams.
int i = 1; // Number of exams.
double avg = 0; // The average of the exams.
Scanner in = new Scanner(System.in);
System.out.print("Please enter the first exam: ");
sum += in.nextInt();
i++;
while(i<=4){
System.out.print("Please enter the next exam: ");
sum += in.nextInt();
if(i==4)
break;// this line is so that it wont increment an extra time.
i++;
}
System.out.println("The total sum for all " + i +" exams is " + sum);
avg = ((double)sum/i);
System.out.println("The average for the exams entered is" + avg);
} // end main ()
} // end class ExamsFor4