I am posting the full code only for contextual purposes. The issue I am having lies in the portion highlighted in the snippet below the full code.
import java.util.Scanner;
public class GradeAvg {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numStudents=0;
int Males=0;
int Females=0;
int maleAvg=0;
int femaleAvg=0;
String Gender="";
System.out.println("How many students?");
numStudents=input.nextInt();
for (int n=1; n <= numStudents; n++) {
System.out.println("Is student "+ n +" male or female (M/F)?");
Gender=input.nextLine();
input.nextLine();
if (Gender.equalsIgnoreCase("M")) {
for (int malenum=n; malenum <= numStudents; Males++); {
System.out.println("What is student "+ n +"'s average?");
maleAvg=input.nextInt();
Males++;
}
}
else if (Gender.equalsIgnoreCase("F")) {
for (int femalenum=n; femalenum <= numStudents; Females++) {
System.out.println("What is student "+ n +"'s average?");
femaleAvg=input.nextInt();
Females++;
}
}
maleAvg=numStudents/maleAvg;
femaleAvg=numStudents/femaleAvg;
}
System.out.println("The " + Males +" male students average is: " + maleAvg);
System.out.println("The " + Females +" female students average is: " + femaleAvg);
}
}
Snippet:
System.out.println("How many students?");
numStudents=input.nextInt();
for (int n=1; n <= numStudents; n++) {
System.out.println("Is student "+ n +" male or female (M/F)?");
Gender=input.nextLine();
input.nextLine();
if (Gender.equalsIgnoreCase("M")) {
for (int malenum=n; malenum <= numStudents; Males++); {
System.out.println("What is student "+ n +"'s average?");
maleAvg=input.nextInt();
Males++;
}
}
else if (Gender.equalsIgnoreCase("F")) {
for (int femalenum=n; femalenum <= numStudents; Females++) {
System.out.println("What is student "+ n +"'s average?");
femaleAvg=input.nextInt();
Females++;
}
}
When the full code is ran, I am prompted with the question "How many students?" This is correct, and I enter and integer such as "5". I then am prompted with the question "Is student 1 male or female (M/F)?" to which i reply "M".
It then (for some unknown reason) skips over the rest of the loop, and returns back to the question "Is student 2 male or female (M/F)?". I am not sure why this is happening, and I do not need the complete answer to this code as it is a homework problem. I simply need the explanation as to why my loop is working improperly, and how to get the prompts to pop up in the if statements in the loop. I really appreciate any and all help from everyone who chooses to comment.
Aucun commentaire:
Enregistrer un commentaire