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

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

问题描述

我只是编程的初学者,所以我找不到解决方法.现有的问题似乎要么是其他语言的问题,要么是让我烦恼的问题.

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语句中指向月份".在任何情况下的初始化都是无效的吗?我在开关外声明了month变量...

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中的开关内初始化变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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