-1

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.

Flimzy
  • 60,850
  • 13
  • 104
  • 147
F-H
  • 47
  • 6

1 Answers1

1

You missed to re-assign value at the end of while-loop:

    System.out.println("Vul de volgende naam in");
    naam = A.nextLine();/* changed */
}
  • Thanks, that did it. Tried it earlier but put "string naam = .." and it gave me a bug – F-H Sep 23 '18 at 16:07
  • 1
    @F-H, that is not a bug, it is an error as you can't declare multiple times with same name in the same block. –  Sep 23 '18 at 16:16
  • `nextLine()` after `nestDouble()` will not work (at least [not on first call](https://stackoverflow.com/q/13102045)). Using `next()` as in OP example was probably correct choice. – Pshemo Sep 23 '18 at 16:56