只是想知道我该如何打印。用户输入条件是否正确?尝试稍后打印出来以显示结果

if(SkillLevel > 1) {
        System.out.println("Would you like to sign up for medical y/n?");
        Medical = sc.next().charAt(0);
        if (Medical == 'y') {
            TotalPay = TotalPay -  23.50;
        }

       if(Medical == "y") {
           System.out.println("\nMedical Insurance cost : -$23.50");
       }

}

最佳答案

如果在第一个char'y'的情况下同时进行显示和计算,则只需要第一个if语句。

不仅如此,第二个if语句也永远不会为真,因为类型不兼容(Stringchar)。它甚至不应该让您编译。

这是您需要的:

if (SkillLevel > 1) {
    System.out.println("Would you like to sign up for medical y/n?");
    Medical = sc.next().charAt(0); // make sure you define Medical as char
    if (Medical == 'y') {
        TotalPay = TotalPay - 23.50;
        System.out.println("\nMedical Insurance cost: -$23.50");
    }
}

10-06 14:48