英文:
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("The average grade is " + test.calcAvgGrade() + ".");
}
}
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("How many students are enrolled?");
this.numOfStudents = input.nextInt();
return;
}
public void getGrades() {
for(int i = 1; i == this.numOfStudents; i++) {
System.out.print("Enter the grade of student # " + i + ":");
this.totalGrade = input.nextDouble() + this.totalGrade;
System.out.println("");
}
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<=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 <= this.numOfStudents; i++) {
System.out.print("Enter the grade of student # " + i + ":");
this.totalGrade = input.nextDouble() + this.totalGrade;
System.out.println("");
}
Because of i == this.numOfStudents
, you're having issues.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论