关于在切换语句中使用三元运算符的问题[重复]

问题描述 投票:0回答:2

我正在研究if,else,switch,三元运算符。我试图从用户的输入中获取月份,年份并显示月份中的天数。在处理它时,我有一个COMPILE ERROR,但不知道如何解决它。请在下面查看我的代码并给我一些建议。

谢谢,希望大家安全!

import java.util.Scanner;

public class Exercise03_11 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        // Prompt the user to enter three integers
        System.out.print("Enter the month: ");
        int month = input.nextInt();

        System.out.print("Enter the year: ");
        int year = input.nextInt();
        String s = "";
        int days = 0;

        boolean leapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
        switch (month) {
            case 1: days = 31;
                    s = "January";
                    break;
            case 2: (leapYear == true) ? days = 29 : days = 28;
                    s = "Feburary";
                    break;
            case 3: days = 31;
                    s = "March";
                    break;
            case 4: days = 30;
                    s = "April";
                    break;
            case 5: days = 31;
                    s = "May";
                    break;
            case 6: days = 30;
                    s = "June";
                    break;
            case 7: days = 31;
                    s = "July";
                    break;
            case 8: days = 31;
                    s = "Auguest";
                    break;
            case 9: days = 30;
                    s = "September";
                    break;
            case 10: days = 31;
                    s = "October";
                    break;
            case 11: days = 30;
                    s = "November";
                    break;
            case 12: days = 31;
                    s = "December";
                    break;
        }

        System.out.println(s + " " + year + " has " + days + " days.");

    }
}

它显示错误如下

Exercise03_11.java:21: error: unexpected type
            case 2: (leapYear == true) ? days = 29 : days = 28;
                                       ^
  required: variable
  found:    value
1 error
java switch-statement conditional-operator
2个回答
0
投票

三元运算符产生表达式,即值。它不是一个声明。该代码应改为:

days = leapYear ? 29 : 28;

[注意:无需将布尔值与true进行比较,它已经是布尔值,可以按原样在三元运算符(或if()条件)中使用。

可以查看this教程以了解更多详细信息。

PS 2月,不是2月。


0
投票

问题出在三元运算符上,您需要对其进行更改,以及它的工作方式:

Scanner input = new Scanner(System.in);

            // Prompt the user to enter three integers
            System.out.print("Enter the month: ");
            int month = input.nextInt();

            System.out.print("Enter the year: ");
            int year = input.nextInt();
            String s = "";
            int days = 0;

            boolean leapYear = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
            switch (month) {
                case 1: days = 31;
                        s = "January";
                        break;
                case 2: days = leapYear ? 29 : 28;
                        s = "Feburary";
                        break;
                case 3: days = 31;
                        s = "March";
                        break;
                case 4: days = 30;
                        s = "April";
                        break;
                case 5: days = 31;
                        s = "May";
                        break;
                case 6: days = 30;
                        s = "June";
                        break;
                case 7: days = 31;
                        s = "July";
                        break;
                case 8: days = 31;
                        s = "Auguest";
                        break;
                case 9: days = 30;
                        s = "September";
                        break;
                case 10: days = 31;
                        s = "October";
                        break;
                case 11: days = 30;
                        s = "November";
                        break;
                case 12: days = 31;
                        s = "December";
                        break;
            }

            System.out.println(s + " " + year + " has " + days + " days.");


}
}

输出:

Enter the month: 06
Enter the year: 2020
June 2020 has 30 days.
© www.soinside.com 2019 - 2024. All rights reserved.