我有一项任务我在这里苦苦挣扎。这是一个基于循环的作业,对我来说有点模糊。作业如下:
除了#4的开头部分之外,我并没有挣扎。我不完全确定如何创建一个为所提交的任务分配运行的循环?
这就是我到目前为止所做的,我主要是根据提交了多少作业来重复循环...我尝试过,我只是想不出来!我很有信心我可以做其余的事情:))
String studentName = " ";
String courseName = " ";
int assign = 0;
int loop = 0;
int totalScore = 0;
int scorethisAssign = 0;
System.out.println("Enter student name:");
studentName = input.next();
System.out.println("Enter course name:");
courseName = input.next();
System.out.println("How many assignments have you submitted:");
assign = input.nextInt();
while (assign <= 0) {
System.out.println(" ");
System.out.println("You must enter a number greater than 0 -- TRY AGAIN!");
System.out.println("How many assignments have you submitted:");
assign = input.nextInt();
}
while (assign > loop);
{
System.out.println("How many points was assignment "loop + "worth:");
scorethisAssign = input.nextInt();
totalScore = scorethisAssign + totalScore;
loop++;
}
这基本上是程序输出的内容:
Enter student name: Prince Harry
Enter course name: Intro to College Life
How many assignments have you submitted: 4
How many points was assignment 1 worth: 100
How many points did you score: 45
How many points was assignment 2 worth: 75
How many points did you score: 46
How many points was assignment 3 worth: 100
How many points did you score: 83
How many points was assignment 4 worth: 100
How many points did you score: 74
Progress Report for Prince Harry
Course Name is Intro to College Life
-------------------------------------------------
Number of assignments submitted.....4
Total points possible...............375.00
Total points earned.................248.00
Total percent to date...............66.13%
Letter grade to date................D
-------------------------------------------------
Enter yes if there is another class you want to calculate: no
答案 0 :(得分:2)
删除;
末尾的while(assign > loop)
,否则您将在那里进行无限循环。
答案 1 :(得分:1)
我会说你的作业检查的for
循环更易读,可能会减少麻烦。由于您有assign
作为分配数,因此您可以使用该变量来注释for
循环中的每个分配。
此外,您目前的解决方案似乎也有效。