I need to compose a while loop that basically fits as a average student grade calculator. First, it asks for a name, if that name is "Stop" the program will stop running and show how many student passed and how many didn't.
However if the name is anything but "Stop" it will ask you for 4 grades and calculate the average of those grades. After which it will determine weather the student passed or not.
Below is my code:
Scanner A = new Scanner(System.in);
System.out.println("Vul de naam van de student in");
String naam = A.next();
int geslaagd = 0;
int gezakt = 0;
while (!naam.equalsIgnoreCase("stop")){
System.out.println("Vul cijfer 1 in");
double cijfer1 = A.nextDouble();
System.out.println("Vul cijfer 2 in");
double cijfer2 = A.nextDouble();
System.out.println("Vul cijfer 3 in");
double cijfer3 = A.nextDouble();
System.out.println("Vul cijfer 4 in");
double cijfer4 = A.nextDouble();
double gem = ((cijfer1+cijfer2+cijfer3+cijfer4) / 4);
if ((cijfer1 >=4.5) && (cijfer2 >=4.5) && (cijfer3 >=4.5) && (cijfer4 >=4.5) && (gem >=5.5)) {
System.out.println("Gefeliciteerd "+naam+", je gemiddelde is = "+gem+"! GESLAAGD!!!");
geslaagd++;
}
else {
System.out.println("Helaas "+naam+" je bent gezakt.");
gezakt++;
}
System.out.println("Vul de volgende naam in");
A.next();
}
if (naam.equalsIgnoreCase("stop")){
System.out.println("Aantal geslaagden = " + geslaagd);
System.out.println("Aantal gezakten = " + gezakt);
Sorry for the Dutch code.
The problem is that when I type in "Stop", the program doesn't terminate, I know I'm doing something wrong, I just can't quiet grasp it.
It appears I entered a wrong input to try and stop the program. Other than that there was a basic error in my code where I should have changed the String naam = A.next();
to String naam = A.nextLine();
, as a user pointed out. Good thing I switched to web development!