java - for循环结构

标签 java

该程序将通过提示使用 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 .只要有可能,您应该更喜欢 doubleDouble

另见

相关问题


重写

这里有一种重写代码以使其更具可读性的方法。我用了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 中的常量都是大写

相关问题

关于java - for循环结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3223651/

相关文章:

java - 将 ListView 项目背景更改为渐变

java - JSF,如何在不同的页面/支持 bean 中设置属性,然后导航到该页面?

java - 如何创建在打瞌睡模式和省电模式下都能工作的 PhoneStateListener?

Java获取方法移动到前面而不是后面

java - JTable 中的条件 JComboBox - 如何自动更改值

java - 访问 popupWindow 以从不同的 void 中关闭

java - 多个 WebSecurityConfigurerAdapters : JWT authentication and form login in spring security

Java模型对象设计

java - JSON 模型避免项目异常

java - 如果 is=false,Java 是否浪费时间检查 "if(isOK && conditionA)"中的 conditionS?