用于循环结构

时间:2010-07-11 15:55:05

标签: 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'之间,但是隔离了它。

谢谢,来自程序员世界的网络家伙陷阱。

5 个答案:

答案 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的框。您应该尽可能选择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);
    }
}

示例会话如下:

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中的常量全部为大写

相关问题

答案 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