0

Help- super stumped on my loop...

I need to create a loop so the amount that you input for example 5 students inputted, will corrospond to a grade. I have done the code, I just don't understand how I can get the input to corrospond to the grades. Here is what I have so far:

public class practise {

  public static void main(String[] args) {
        Scanner in = new Scanner (System.in); 
        int i = 0; 
        int sum = 0; 

        System.out.println("Enter the number of students: ");
        int students = in.nextInt(); 

        System.out.println("Enter exam score of each student:");
        int score = in.nextInt(); 

    for (; students < score; students++) {    
     int n = in.nextInt();
     sum+=n;

     if (n >= 41) {
     System.out.println("Invalid exam scores, reenter: ");      
     }
  }
  if (score <= 40 && score >= 36 ) {
       System.out.println("Grade Score: 5");

      } if (score <= 35 && score >= 30) {
       System.out.println("Grade Score: 4");

      } if (score <= 29 && score >= 20) {
       System.out.println("Grade Score: 3");

      } if (score <= 19 && score >= 0 ) {
       System.out.println("Grade Score: U");

      }
}
 }

What is it that I need to do to get this to work?

2
Contributors
3
Replies
29
Views
2 Months
Discussion Span
Last Post by ItsAmi
0

I have thought about it, I will need to assign each student entered a number in the loop

so student 1 (0) student 2 (1) student 3 (2) ext. That much I understand but then how can I get this to corrospond to the grade?

2

Your code is almost almost there...
Here's a simple way to structure it (pseudo code)

ask for number of students
for n = 1 to number of students {
   ask for student n's score
   calculate/display grade
} 

so all you need to do is fix your for statement and move entering the score to inside the loop. Fixing your indentation will be an enormous help in seeing what's happening!

Edited by JamesCherrill: Fixed typo in for stmt

0

Hi James- thank you for your help, I really appriciate it and the positive words! :)

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.