为什么我不能在 Java 中的 switch 中初始化变量? [英] Why can't I initialize a variable inside a switch in Java?

查看:48
本文介绍了为什么我不能在 Java 中的 switch 中初始化变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是编程的初学者,所以我找不到解决方法.现有的问题似乎不是用其他语言写的,就是超出了我的想象.

I am just a beginner at programming, so I couldn't find my way around this. Existing questions seem to be either in other languages, or going over my head.

我正在尝试编写一个小程序来返回任何给定日期输入的特定星期几.

I am trying to write a small program for returning the week-specific day for any given date input.

import java.util.*;

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

    Scanner yearfinder = new Scanner(System.in);
    System.out.println("Please type any year");
    int year = yearfinder.nextInt();

    Scanner monthfinder = new Scanner(System.in);
    System.out.println("Please type any month");
    String textmonth = monthfinder.nextLine();
    int month;

    switch(textmonth) {
        case ("january"):
            month = 1;
            break;
        case ("february"):
            month = 2;
            break;
        case ("march"):
            month = 3;
            break;
        case ("april"):
            month = 4;
            break;
        case ("may"):
            month = 5;
            break;
        case ("june"):
            month = 6;
            break;
        case ("july"):
            month = 7;
            break;
        case ("august"):
            month = 8;
            break;
        case ("september"):
            month = 9;
            break;
        case ("october"):
            month = 10;
            break;
        case ("november"):
            month = 11;
            break;
        case ("december"):
            month = 12;
            break;
        default:
            System.out.println("The month you input was invalid");
        }

    Scanner datefinder = new Scanner(System.in);
    System.out.println("Please type any day");
    int date = datefinder.nextInt();

    System.out.println("The date you gave was " + date + "/" + month + "/" + year);
}
}

    //Jan 1 1900 was a monday.

编译它给我一个错误,指出变量月份可能尚未初始化",同时在 println 语句中指向月份".任何情况下的初始化是否无效或什么?我在开关外声明了月份变量...

Compiling this gives me an error that states that "variable month may not have been initialized", while pointing at "month" in the println statement. Is the initialization inside any of the cases invalid or something? I declared the month variable outside the switch...

推荐答案

与其他回复相反,声明时不需要初始化month.

Contrary to other replies, you do not need to initialize month at declaration.

问题在于,如果 textmonth 不是文字值,则执行将落入 default 的情况,即月份没有被初始化.

The problem is that if the textmonth is none of the literal values, the execution will fall to the default case, where month does not get initialized.

您可以将其初始化为无效值,例如 default 情况下的 0,但也许更好的选择是给出错误消息并中止执行.

You could initialize it to an invalid value, say 0 in the default case, but perhaps a better option is to give an error message and abort execution.

这篇关于为什么我不能在 Java 中的 switch 中初始化变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆