Java 错误:变量digitMonth可能尚未初始化

Java 错误:变量digitMonth可能尚未初始化,java,if-statement,initialization,Java,If Statement,Initialization,我正试图写一个程序,用出生年份、月份和日期来计算生日在哪一天。问题是,我收到了以下错误消息: 错误:变量digitMonth可能尚未初始化 总计=输入年+测试2+数字月+输入日; ^ 我不知道怎么修理它。将digitMonth设置为一个数字(例如1)可以使程序工作,但对于公式,每个月需要不同的数字(1表示1月,4表示2月,4表示3月,0表示4月,等等) 我已经查看了其他问题中类似的错误,但我还没有发现任何有用的东西 帮忙 import java.util.Scanner; 公共阶层出生{ 公共静

我正试图写一个程序,用出生年份、月份和日期来计算生日在哪一天。问题是,我收到了以下错误消息:

错误:变量digitMonth可能尚未初始化 总计=输入年+测试2+数字月+输入日; ^

我不知道怎么修理它。将digitMonth设置为一个数字(例如1)可以使程序工作,但对于公式,每个月需要不同的数字(1表示1月,4表示2月,4表示3月,0表示4月,等等)

我已经查看了其他问题中类似的错误,但我还没有发现任何有用的东西

帮忙

import java.util.Scanner;
公共阶层出生{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
int inputYear、inputMonth、inputDay、digitMonth、test2、total、dayNum;
System.out.println(“输入您出生年份的最后两位数字”);
inputYear=scan.nextInt();
System.out.println(“输入您出生的月份号”);
inputMonth=scan.nextInt();
System.out.println(“输入您的出生日期”);
inputDay=scan.nextInt();
测试2=输入年份/4;
如果(inputMonth>0&&inputMonth<2)
{
数字月=1;
}
否则如果(inputMonth>1&&inputMonth<3)
{
数字月=4;
}
否则如果(inputMonth>2&&inputMonth<4)
{
数字月=4;
}
否则如果(inputMonth>3&&inputMonth<5)
{
数字月=0;
}
否则如果(inputMonth>4&&inputMonth<6)
{
数字月=2;
}
否则如果(inputMonth>5&&inputMonth<7)
{
数字月=5;
}
否则如果(inputMonth>6&&inputMonth<8)
{
数字月=0;
}
否则如果(inputMonth>7&&inputMonth<9)
{
数字月=3;
}
否则如果(inputMonth>8&&inputMonth<10)
{
数字月=6;
}
否则如果(inputMonth>9&&inputMonth<11)
{
数字月=1;
}
否则如果(inputMonth>10&&inputMonth<12)
{
数字月=4;
}
否则如果(inputMonth>11&&inputMonth<13)
{
数字月=6;
}
其他的
System.out.println(“你搞砸了”);
总计=输入年+测试2+数字月+输入日;
dayNum=总数/7;
如果(dayNum>0 | | dayNum<2)
{
System.out.println(“你出生在周日”);
}
否则如果(dayNum>1 | | dayNum<3)
{
System.out.println(“你出生在星期一”);
}
否则如果(dayNum>2 | | dayNum<4)
{
System.out.println(“你出生在星期二”);
}
否则如果(dayNum>3 | | dayNum<5)
{
System.out.println(“你出生在星期三”);
}
否则如果(dayNum>4 | | dayNum<6)
{
System.out.println(“你出生在星期四”);
}
否则如果(dayNum>5 | | dayNum<7)
{
System.out.println(“你出生在星期五”);
}
else if(dayNum>-1 | | dayNum<1)
{
System.out.println(“你出生在周六”);
}
}

}您只在
if
else if
语句中为
digitMonth
赋值,而不是在最后的
else
语句中。
那么,如果
if
else if
都不成功,而只有
else
部分成功,会发生什么呢<这个 那么,
digitMonth
的值是多少?

回答:

这可能永远不会发生在您的代码中,但编译器对此表示不满

有两种解决方案:

1)只需给
digitMonth
一个初始值即可。i、 e.
int数字月=0

2)在最终else中初始化
digitMonth
。i、 e:

else{
    //some code
    digitMonth = 0;
}

希望这对您的代码有所帮助,有可能在为其指定值之前使用
digitMonth
。一个选项是将
digitMonth
设置为默认值:

int digitMonth = 0;
另一种选择是为
else
分支中的
digitMonth
赋值:

...
else {
    System.out.println("Invalid month");
    digitMonth = 0;
}
...
else {
    return;
    // OR throw a RuntimeException 
    // throw new IllegalArgumentException
}
您还可以停止
else
分支中的控制流:

...
else {
    System.out.println("Invalid month");
    digitMonth = 0;
}
...
else {
    return;
    // OR throw a RuntimeException 
    // throw new IllegalArgumentException
}

您还需要将
digitmount
初始化为
zero
我认为您的代码中也有不正确的逻辑:

您应该有:
dayNum=total%7

希望以下代码可以正常工作:

public static void main(String args[]) {

        Scanner scan = new Scanner(System.in);
        int inputYear, inputMonth, inputDay, digitMonth = 0, test2, total, dayNum;

        System.out.println("Enter the last two digits of the year that you were born in");
        inputYear = scan.nextInt();
        System.out.println("Enter the month number that your were born in");
        inputMonth = scan.nextInt();
        System.out.println("Enter your birth day");
        inputDay = scan.nextInt();

        test2 = inputYear / 4;

        if (inputMonth > 0 && inputMonth < 2)
        {
            digitMonth = 1;
        }
        else if (inputMonth > 1 && inputMonth < 3)
        {
                digitMonth = 4;
        }
        else if (inputMonth > 2 && inputMonth < 4)
        {
                digitMonth = 4;
        }
        else if (inputMonth > 3 && inputMonth < 5)
        {
                digitMonth = 0;
        }
        else if (inputMonth > 4 && inputMonth < 6)
        {
                digitMonth = 2;
        }
        else if (inputMonth > 5 && inputMonth < 7)
        {
                digitMonth = 5;
        }
        else if (inputMonth > 6 && inputMonth < 8)
        {
                digitMonth = 0;
        }
        else if (inputMonth > 7 && inputMonth < 9)
        {
                digitMonth = 3;
        }
        else if (inputMonth > 8 && inputMonth < 10)
        {
                digitMonth = 6;
        }
        else if (inputMonth > 9 && inputMonth < 11)
        {
                digitMonth = 1;
        }
        else if (inputMonth > 10 && inputMonth < 12)
        {
                digitMonth = 4;
        }
        else if (inputMonth > 11 && inputMonth < 13)
        {
                digitMonth = 6;
        }
        else
            System.out.println("You fuck-up");


        total = inputYear + test2 + digitMonth + inputDay;
        dayNum = total % 7;

        switch(dayNum){
        case 1:System.out.println("You were born on a Sunday");break;
        case 2:System.out.println("You were born on a Monday");break;
        case 3: System.out.println("You were born on a Tuesday");break;
        case 4:System.out.println("You were born on a Wednesday");break;
        case 5:System.out.println("You were born on a Thursday");break;
        case 6:System.out.println("You were born on a Friday");break;
        case 7:System.out.println("You were born on a Saturday");break;

        }
publicstaticvoidmain(字符串参数[]){
扫描仪扫描=新扫描仪(System.in);
int inputYear、inputMonth、inputDay、digitMonth=0、test2、total、dayNum;
System.out.println(“输入您出生年份的最后两位数字”);
inputYear=scan.nextInt();
System.out.println(“输入您出生的月份号”);
inputMonth=scan.nextInt();
System.out.println(“输入您的出生日期”);
inputDay=scan.nextInt();
测试2=输入年份/4;
如果(inputMonth>0&&inputMonth<2)
{
数字月=1;
}
否则如果(inputMonth>1&&inputMonth<3)
{
数字月=4;
}
否则如果(inputMonth>2&&inputMonth<4)
{
数字月=4;
}
否则如果(inputMonth>3&&inputMonth<5)
{
数字月=0;
}
否则如果(inputMonth>4&&inputMonth<6)
{
数字月=2;
}
否则如果(inputMonth>5&&inputMonth<7)
{
数字月=5;
}
否则如果(inputMonth>6&&inputMonth<8)
{
数字月=0;
}
否则如果(inputMonth>7&&inputMonth<9)
{
数字月=3;
}
否则如果(inputMonth>8&&inputMonth<10)
{