简体   繁体   中英

Beginner Java Loops

I have an assignment I'm struggling with quite a bit here. It's an assignment based on loops, which are a little blurry to me. The assignment is as follows:

  1. Ask the student name.
  2. Ask the course name (cannot be less than 0 --> if so, error message.)
  3. Ask how many assignments they submitted.
  4. Based on #3, ask a few questions such as total points possible and earned to generate a progress report.

I'm not struggling with anything besides the beginning part of #4. I am not entirely sure how to create a loop that runs for as many assignments that were submitted?

This is what I have so far, I'm mostly struggling with the loop that repeats based on how many assignments were submitted...I tried, I just can't figure it out! I'm pretty confident I can do the rest :)

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++;
}

This is essentially what the program would output to:

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

Remove the ; at the end of while(assign > loop) , otherwise you're going to have an infinite loop there.

I would say a for loop for your assignment checking is better readable and probably will give you less of hassles. Since you have assign being the number of assignments, you could use that variable to annotate each assignment in your for loop. Besides, your current solution seems to work as well.

Java 'for' statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM