为什么这个简单的Java for循环不起作用?

huangapple go评论168阅读模式
英文:

Why isn't this simple Java for loop working?

问题

以下是翻译好的内容:

主类:

class Main {
  public static void main(String[] args) {
    Test test = new Test();
    test.getNumOfStudents();
    test.getGrades();
    System.out.println("平均成绩为:" + test.calcAvgGrade() + "。");
  }
}

测试类:

import java.util.*;

public class Test {
  
  public int numOfStudents;
  public double averageGrade;
  public double totalGrade;
  public Scanner input = new Scanner(System.in);

  public void getNumOfStudents() {
    System.out.println("有多少名学生已注册?");
    this.numOfStudents = input.nextInt();
    return;
  }

  public void getGrades() {
    for(int i = 1; i <= this.numOfStudents; i++) {
      System.out.print("请输入第 " + i + " 名学生的成绩:");
      this.totalGrade = input.nextDouble() + this.totalGrade;
      System.out.println("");
    }
    return;
  }

  public double calcAvgGrade() {
    this.averageGrade = this.totalGrade / this.numOfStudents;
    return this.averageGrade;
  }

}
英文:

I'm trying to figure out why my test program seems to be completely skipping a public class method when being called in an instantiated object in the main method. Please see below for both the "Test" class and the "Main" class where the main method creates the object and attempts to call all three class methods.

Main class:

class Main {
  public static void main(String[] args) {
    Test test = new Test();
    test.getNumOfStudents();
    test.getGrades();
    System.out.println(&quot;The average grade is &quot; + test.calcAvgGrade() + &quot;.&quot;);
  }
}

Test Class:

import java.util.*;

public class Test {
  
  public int numOfStudents;
  public double averageGrade;
  public double totalGrade;
  public Scanner input = new Scanner(System.in);

  public void getNumOfStudents() {
    System.out.println(&quot;How many students are enrolled?&quot;);
    this.numOfStudents = input.nextInt();
    return;
  }

  public void getGrades() {
    for(int i = 1; i == this.numOfStudents; i++) {
      System.out.print(&quot;Enter the grade of student # &quot; + i + &quot;:&quot;);
      this.totalGrade = input.nextDouble() + this.totalGrade;
      System.out.println(&quot;&quot;);
    }
    return;
  }

  public double calcAvgGrade() {
    this.averageGrade = this.totalGrade / this.numOfStudents;
    return this.averageGrade;
  }

}

答案1

得分: 2

当你运行你的for循环时,你的条件是i==this.numOfStudents。我认为你想让它达到this.numOfStudents

所以要修复这个问题,你应该使用小于或等于的条件。所以你的for循环应该是i<=this.numOfStudents,因为现在你的for循环只在i==this.numOfStudents时运行。

英文:

When you run your for loop, you have the condition be i==this.numOfStudents. I think you are trying to make it up to this.numOfStudents.

So to fix this you would have a less than or equal to. So you would have i&lt;=this.numOfStudents, because right now your for loop is saying run ONLY if i==this.numOfStudents.

答案2

得分: 0

在方法 getGrades 中,for 循环应为:

for(int i = 1; i <= this.numOfStudents; i++) {
    System.out.print("输入第 " + i + " 位学生的成绩:");
    this.totalGrade = input.nextDouble() + this.totalGrade;
    System.out.println("");
}

因为 i == this.numOfStudents,所以你遇到了问题。

英文:

In method getGrades, the for loop should be:


   for(int i = 1; i &lt;= this.numOfStudents; i++) {
         System.out.print(&quot;Enter the grade of student # &quot; + i + &quot;:&quot;);
         this.totalGrade = input.nextDouble() + this.totalGrade;
         System.out.println(&quot;&quot;);
       }

Because of i == this.numOfStudents, you're having issues.

huangapple
  • 本文由 发表于 2020年8月21日 11:04:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63515932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定