Java程序使用一年中的季节开关得到错误

rslzwgfq  于 2023-01-01  发布在  Java
关注(0)|答案(2)|浏览(107)

我正在试用一个程序,下面是详细信息
写一个程序,取一个月份号,如果是夏天,冬天,雨季,就打印月份号-112。11月、12月、2月、夏季:三月、四月、五月、六月、七月,雨天:七月、八月、九月、十月
注意:你可以使用默认值来处理雨天,而冬季和夏季只需要2种情况

#解决方案1 -仅测试逻辑

package java_cls;

public class switch {
    public static void main (String[] args) {
        int month = 6;
        switch (month) {
            case 1:
                System.out.println("January");
                System.out.println("Winter");
                break;
            case 2:
                System.out.println("February");
                System.out.println("Winter");
                break;
            case 3:
                System.out.println("March");
                System.out.println("Summer");
                break;
            case 4:
                System.out.println("April");
                System.out.println("Summer");
                break;
            case 5:
                System.out.println("May");
                System.out.println("Summer");
                break;
            case 6:
                System.out.println("June");
                System.out.println("Summer");
                break;
            case 7:
                System.out.println("July");
                System.out.println("Summer");
                break;
            case 8:
                System.out.println("August");
                System.out.println("Rainy");
                break;
            case 9:
                System.out.println("September");
                System.out.println("Rainy");
                break;
            case 10:
                System.out.println("October");
                System.out.println("Rainy");
                break;
            case 11:
                System.out.println("November");
                System.out.println("Winter");
                break;
            case 12:
                System.out.println("December");
                System.out.println("Winter");
                break;

        }

    }
}

#溶液2 -实际溶液

public class ifelse {
    public static void main (String[] args) {

        String summer = "March,April,May,June,July";
        String winter = "November,December,January,February";
        String rainy = "August,September,October";

        int month = 1;

        switch (summer) {
            case 1:
                System.out.printf("Summer");
                break;
        }
        switch (winter) {
            case 2:
                System.out.printf("Winter");
                break;
        }
        switch (rainy) {
            case 3:
            default:

        }
    }
}

从解决方案2我无法构建最终的解决方案,任何线索将是有帮助的。

bfnvny8b

bfnvny8b1#

写一个程序,以采取一个月的数字和打印,如果它是夏季,冬季,雨季月份数字-1至12让我们假设冬季:11月、12月、2月夏季:3月、4月、5月、6月、7月多雨:七月、八月、九月、十月
注意:你可以使用默认值来处理雨天,而冬季和夏季只需要2种情况
使用switch-语句和字面上只有2 case标签是无法做到这一点的,但是Java允许对case标签进行分组,并将标签组与单个语句相关联。
因此,正如*@Old Dog Programmer * 在评论中指出的,您可以创建case标签的2组(针对"Winter""Summer"),并使用default标签覆盖"Rainy"季节。
为此,您需要定义一个包含月份名称的数组,然后在switch中引用其元素。
它看起来可能是:

int month = // initalizing the month variable
        
String[] months = {
    "January", "February",                   // Winter
    "March", "April", "May", "June", "July", // Summer
    "August", "September", "October",        // Rainy
    "November", "December"                   // Winter
};
        
int monthIndex = month - 1;
        
switch (monthIndex) {
    case 0: case 1: case 10: case 11:
        System.out.println(months[monthIndex]);
        System.out.println("Winter");
        break;
    case 2: case 3: case 4: case 5: case 6:
        System.out.println(months[monthIndex]);
        System.out.println("Summer");
        break;
    default:
        System.out.println(months[monthIndex]);
        System.out.println("Rainy");
}

我想这就是你必须要做的。
仅供参考,这里有几个更高级的选项。
∮ ∮ ∮
上面显示的switch-语句可以使用switch-表达式以更简洁的方式编写(无需重复case和使用break语句):

switch (monthIndex) {
    case 0, 1, 10, 11 -> {
        System.out.println(months[monthIndex]);
        System.out.println("Winter");
    }
    case 2, 3, 4, 5, 6 -> {
        System.out.println(months[monthIndex]);
        System.out.println("Summer");
    }
    default -> {
        System.out.println(months[monthIndex]);
        System.out.println("Rainy");
    }
}

Map

另一个将月份与相应季节相关联的选项是使用Map,这种方法不需要诉诸条件语句。
为了生成Map的代码,我使用了流API和收集器toMap()
总体逻辑保持不变:Map包含有关"Winter""Summer"的信息,使用Map.getOrDefault()方法覆盖"Rainy"季节的情况。
下面是它可能的样子:

public static final String[] MONTHS = {
    "January", "February",                   // Winter
    "March", "April", "May", "June", "July", // Summer
    "August", "September", "October",        // Rainy
    "November", "December"                   // Winter
};

public static final Map<Integer, String> SEASON_BY_MONTH =
    Stream.concat(
            IntStream.of(0, 1, 10, 11).mapToObj(i -> Map.entry(i, "Winter")),
            IntStream.of(2, 3, 4, 5, 6).mapToObj(i -> Map.entry(i, "Summer"))
        )
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

public static void displayMonth(int month) {
    int monthIndex = month - 1;
    
    System.out.println(MONTHS[monthIndex]);
    System.out.println(SEASON_BY_MONTH.getOrDefault(monthIndex, "Rainy"));
}
whlutmcx

whlutmcx2#

因为问题的解决似乎不需要月份的名称,所以如果您真的只需要switch语句中的两个case和一个default,可以这样做

public class Main {
    public static void main(String[] args) {
            int monthNum = 1;
            Season season = Season.getSeason(monthNum);
            if(season == null) return;
            switch (season) {
                case SUMMER:
                    System.out.println("Summer");
                    break;
                case WINTER:
                    System.out.println("Winter");
                    break;
                default:
                    System.out.println("Rainy");
                    break;
            }
        }
}

--

enum Season {
        WINTER(List.of(1, 2, 11, 12)),
        SUMMER(List.of(3, 4, 5, 6, 7)),
        RAINY(List.of(8, 9, 10));

        private final List<Integer> months;

        Season(List<Integer> months) {
            this.months = months;
        }

        public static Season getSeason(int num) {
            for (Season season : Season.values()) {
                if (season.months.contains(num)) return season;
            }
            return null;
        }
    }
  • -此选项仅在您还想打印月份名称时使用
public class Main {
    public static void main(String[] args) {
        String[] months = new String[]{
                "January", "February", "March", "April",
                "May", "June", "July", "August",
                "September", "October", "November", "December"};
        int monthNum = 12;
        Season season = Season.getSeason(monthNum);
        if (season == null) return;
        switch (season) {
            case SUMMER:
                System.out.println("Summer");
                System.out.println(months[monthNum - 1]);
                break;
            case WINTER:
                System.out.println("Winter");
                System.out.println(months[monthNum - 1]);
                break;
            default:
                System.out.println("Rainy");
                System.out.println(months[monthNum - 1]);
                break;
        }
    }
}

相关问题