该程序将通过提示使用 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'
之间,但这将它隔离了。
谢谢,来自程序员世界中的 Network Dude 陷阱。
最佳答案
您遇到了所谓的差一错误,再加上您不必要地盘绕循环逻辑这一事实。
关于循环,我推荐两件事:
- 不要循环
for (int i = 1; i <= N; i++)
;这是非典型的- 做
for (int i = 0; i < N; i++)
;比较典型
- 做
- 与其检查最后一次迭代来做某事,不如重构并将其移出循环
相关问题
另见
关于 Double Avg
在 Java 中,变量名以小写字母开头。此外,Double
是引用类型,原语的盒子 double
.只要有可能,您应该更喜欢 double
至 Double
另见
- > Java Language Guide/Autoboxing
- > JLS 5.1.7 Boxing Conversion和 5.1.8 Unboxing Conversion
- Effective Java 2nd Edition,第 49 项:优先使用原语而不是盒装原语
相关问题
- > What is the difference between an
int
and anInteger
in Java/C#? - > Java: What’s the difference between autoboxing and casting?
- > Why does
int num = Integer.getInteger(“123”)
throwNullPointerException
? - > Why does autoboxing in Java allow me to have 3 possible values for a
boolean
? - > Is it guaranteed that
new Integer(i) == i
in Java? (是!!!) - > When comparing two Integers in Java does auto-unboxing occur? (不!!!)
- > Java noob: generics over objects only? (是的,不幸的是)
重写
这里有一种重写代码以使其更具可读性的方法。我用了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);
}
}
示例 session 如下:
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
, 这样做- Java 中的常量都是大写
相关问题
- > Why does
(360 / 24) / 60 = 0
in Java- 因为它执行整数除法。这就是为什么转换为
(double)
的原因在上面的代码中除法之前是必要的,以便它执行浮点除法。
- 因为它执行整数除法。这就是为什么转换为
- > How does the ternary operator work?
- 这是
?:
上述代码中的运算符,也称为条件运算符。 - 另请参阅:JLS 15.25 Conditional Operator ?:
- 这是
关于java - for循环结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3223651/