0

It does not allow me to enter my choice whether I want to enter another grade or not, It should allow me to enter my choice of either yes or no and if I enter yes it should allow me to enter another grade except it simply refuses to take user input?

import java.util.Scanner;

public class ExaminationGrades {

    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        double grade;
        String ch;

        do {
            System.out.println("Enter your grade: ");
            grade = scan.nextDouble();

            if (grade <40) {
                System.out.println("you failed");
            } else if (grade >=40 && grade <50) {
                System.out.println("Pass");
            } else if (grade >=50 && grade <60) {
                System.out.println("2:2");
            } else if (grade >=60 && grade <70){
                System.out.println("2:1");
            } else if (grade >=70 && grade <=100){
                System.out.println("1:1");
            } else
                System.out.println("invalid entry");

            System.out.println("Would you like to enter another?");
            ch = scan.nextLine();

        } while(ch =="no");

        scan.close();
    }

}
Walery Strauch
  • 6,792
  • 8
  • 50
  • 57

1 Answers1

0

Change

while(ch =="no");

to

while(ch.equals("yes"))

EDIT

public static void main(String[] args) {
    Scanner scan = new Scanner (System.in);
    double grade;
    String ch;

    do {
        System.out.println("Enter your grade: ");
        grade = scan.nextDouble();
        scan.nextLine(); //Consume the newline character generated by hitting enter

        if (grade <40) {
            System.out.println("you failed");
        }else if (grade >=40 && grade <50) {
            System.out.println("Pass");
        }else if (grade >=50 && grade <60) {
            System.out.println("2:2");
        }else if (grade >=60 && grade <70){
            System.out.println("2:1");
        }else if (grade >=70 && grade <=100){
            System.out.println("1:1");
        }else
            System.out.println("invalid entry");

        System.out.println("Would you like to enter another?");
        ch = scan.nextLine();

    }while(ch.equals("yes")); //Correctly compare strings

    scan.close();
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
  • it still does not allow me to enter user inputs, I cannot enter yes or no it just freezes on "Would you like to enter another? – user3204699 Jan 20 '14 at 00:32
  • Your `scan.nextDouble` does not consume the newline character so you will need to add `scan.nextLine()` right after your `scan.nextDouble()`. Give me a minute and I will put a whole code example. – takendarkk Jan 20 '14 at 00:34
  • @user3204699 This ran just fine for me. Let me know what happens. – takendarkk Jan 20 '14 at 00:40
  • thank you for the help, it works fine, seems like you are the only one who actually bothered helping, so thank you so much. – user3204699 Jan 20 '14 at 00:42