0

我有一个任务,我在这里遇到了很多困难。这是一个基于循环的分配,这对我来说有点模糊。任务如下:

  1. 问学生姓名。
  2. 询问课程名称(不能小于 0 --> 如果是,错误信息。)
  3. 询问他们提交了多少作业。
  4. 根据#3,提出一些问题,例如可能的总分和获得的总分,以生成进度报告。

除了#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
4

2 回答 2

2

删除;末尾的while(assign > loop),否则你将在那里有一个无限循环。

于 2013-11-04T02:41:27.847 回答
1

我会说for你的分配检查循环更具可读性,并且可能会给你带来更少的麻烦。由于您有assign分配的数量,您可以使用该变量来注释for循环中的每个分配。此外,您当前的解决方案似乎也有效。

Java 'for' 语句

于 2013-11-04T02:38:12.853 回答