简体   繁体   中英

Why won't my input Scanner and the rest of my program work?

Could someone help me to fix my code to get it to work. In the console it doesn't allow me to type anything. Could someone test the code on their computer and send me a comment with the fixed code? If not could you just tell me the problem with my code?

String yes= "yes";

    System.out.println("Do you have gym this semester?");
    input.nextLine();
    if (input.equals(yes)){

        int Gym;
        double GymGpa = 0;
        System.out.println("Gym = ");
        Gym= input.nextInt();
        input.close();
        if (Gym >100){
            System.out.println("You have mistyped something");
        }
        if (Gym >= 94 && Gym < 101){
            System.out.println("You have an A");
            GymGpa = 4.0;
            System.out.println(GymGpa);
        }
        if (Gym < 94 && Gym >=90){
            System.out.println("You have an A-");
            GymGpa = 3.7;
            System.out.println(GymGpa);
        }
        if (Gym < 90 && Gym >=87){
            System.out.println("You have a B+");
            GymGpa = 3.3;
            System.out.println(GymGpa);
        }
        if (Gym < 87 && Gym >=80){
            System.out.println("You have a B");
            GymGpa = 3.0;
            System.out.println(GymGpa);
        }
        if (Gym < 80 && Gym >=77){
            System.out.println("You have a B-");
            GymGpa = 2.7;
            System.out.println(GymGpa);
        }
        if (Gym < 77 && Gym >=73){
            System.out.println("You have a C+");
            GymGpa = 2.3;
            System.out.println(GymGpa);
        }
        if (Gym < 73 && Gym >=70){
            System.out.println("You have a C");
            GymGpa = 2.0;
            System.out.println(GymGpa);
        }
        if (Gym < 70 && Gym >=67){
            System.out.println("You have a C-");
            GymGpa = 1.7;
            System.out.println(GymGpa);
        }
        if (Gym < 67 && Gym >=67){
            System.out.println("You have a D+");
            GymGpa = 1.3;
            System.out.println(GymGpa);
        }
        if (Gym < 67 && Gym >=63){
            System.out.println("You have a D");
            GymGpa = 1.0;
            System.out.println(GymGpa);
        }
        if (Gym < 63 && Gym >=60){
            System.out.println("You have a D-");
            GymGpa = 0.7;
            System.out.println(GymGpa);
        }
        if (Gym < 60){
            System.out.println("You have a F");
            GymGpa = 0.0;
            System.out.println(GymGpa);
        }//End of Gym
    }else if (input.equals("no")){

    }

You are comparing a Scanner with a String . You want to store the input as a String before comparing them. Try this:

...
Scanner input = new Scanner(System.in);
String yes = "yes";
String answer = input.nextLine();

if (answer.equals(yes)){
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM