mercredi 3 juin 2020

Program keeps executing the else statement even though the condition of the if statement is being met [duplicate]

/When entering "Y" after being asked if I am a student; the nested if statement should be met giving the corresponding output. However, every time I enter "y" or "Y" after being asked the question; the program still executes the else statement. I don't know what I am doing wrong here./

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);
            ;
        }
    }

    }
}

Aucun commentaire:

Enregistrer un commentaire