即使满足if语句的条件,程序仍会继续执行else语句

时间:2020-06-04 04:12:41

标签: java if-statement

/ 在询问我是否是学生后输入“ Y”时;应满足嵌套的if语句,并提供相应的输出。但是,每次被问到问题后我输入“ y”或“ Y”;程序仍然执行else语句。我不知道我在做什么错。 /

package quiz;
import java.util.Scanner;

public class q {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
int age;
double in;
String choice;
Scanner input = new Scanner(System.in);

System.out.print("How old are you?");
age = input.nextInt();
input.nextLine();
if (age<=18 || age>=65) {

    System.out.print("Are you a student? (Y/N)");
    choice = input.nextLine();
    choice = choice.toUpperCase();

    if (choice == "Y") {
        System.out.println("Your annual fee is $50");

    }

    else {
        System.out.println("Your annual fee is $100");
    }
}


else {
        System.out.print("Whats your income? ");
        in=input.nextDouble();
        input.nextLine();
        System.out.print("Are you a student? (Y/N) ");
        choice = input.nextLine();
        choice = choice.toUpperCase();

        if (choice == "Y") {
            System.out.println("Your annual fee is $"+ in *0.5);

        }
        else {
            System.out.println("Your annual fee is $"+in);
            ;
        }
    }

    }
}

0 个答案:

没有答案