jeudi 27 septembre 2018

While loop creates never ending input loop (Java)

So I want my program to read the inputs "A" "B" or "C" and display the number of each. But I am running into an issue where it never reads and displays my input. My code is as follows:

if (command == 'A'){
                        System.out.println("Type the additional input in a single line.");
                        while(in.hasNext()){
                            String input = in.next().toUpperCase();
                            if(input.equals("A")){ numA++;}
                            if(input.equals("B")){ numB++;}
                            if(input.equals("C")){ numC++;}
}
                            System.out.println("---------------------------------------");
                            System.out.printf("\n%4s      |", "A");
                                for (int a = 1; a <= numA; a++) { System.out.print("*");}
                            System.out.println();
                            System.out.printf("\n%4s      |", "B");
                                for (int b = 1; b <= numB; b++) { System.out.print("*");}
                            System.out.println();
                            System.out.printf("\n%4s      |", "C");
                                for (int c = 1; c <= numC; c++) { System.out.print("*");}
                            System.out.println();
                            double gpa = ((numA * 4)+(numB * 3)+(numC * 2)) / ((numA + numB + numC));
                            System.out.println("GPA: " + gpa);
                            System.out.println();
                            System.out.println("---------------------------------------");
} 

Aucun commentaire:

Enregistrer un commentaire