任何人都可以告诉我为什么它给我变量未初始化? [英] Can anyone show me why does it give me variable not initialized?

查看:86
本文介绍了任何人都可以告诉我为什么它给我变量未初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我尝试编译它时都会显示 d1 变量可能未初始化。我认为问题可能在其他如果。告诉我如何在中运行2个语句,如果,那么。

Every time I try to compile this it shows me d1 variable may not be initialized. I think problem may in the else if. Tell me how to run 2 statements in else if.

import java.util.Scanner;
class IDC {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        System.out.println("                        ");
        System.out.println("Input ID card no...");

        String x = scan.next();


        x = x.substring(0, x.length() - 1); //removing the last char of the string

        String CardNo = x;

        String y = x = x.substring(0, x.length() - 7); //birthday

        y = "19" + y; //birth year = y




        String CardNO1 = CardNo.substring(0, CardNo.length() - 4);

        //System.out.println(CardNO1);

        CardNO1 = CardNO1.substring(2);

        //System.out.println(CardNO1);

        //gender

        int g = Integer.parseInt(CardNO1); //converting string to int
        String G;
        if (g < 500) {
            G = "Male";
        } else {
            G = "female";
        }
        //System.out.println(G);
        double C = Integer.parseInt(CardNO1);
        if (C > 500) {
            C = C - 500;
        } else {
            C = C;
        }
        //calculating month and the day of birth

        double d1;
        int Month;
        //

        if (C < 31) {
            Month = 1;
            d1 = C;
        } else if (C <= 60) {
            Month = 2;
            d1 = C - 31;
        } else if (C <= 91) {
            Month = 3;
            d1 = C - 60;
        } else if (C <= 121) {
            Month = 4;
            d1 = C - 91;
        } else if (C <= 152) {
            Month = 5;
            d1 = C - 121;
        } else if (C <= 182) {
            Month = 6;
            d1 = C - 152;
        } else if (C <= 213) {
            Month = 7;
            d1 = C - 182;
        } else if (C <= 244) {
            Month = 8;
            d1 = C - 213;
        } else if (C <= 274) {
            Month = 9;
            d1 = C - 244;
        } else if (C <= 305) {
            Month = 10;
            d1 = C - 274;
        } else if (C <= 335) {
            Month = 11;
            d1 = C - 305;
        } else if (C <= 366) {
            Month = 12;
            d1 = C - 335;
        } else {
            Month = 00;
        }
        //double d1;

        System.out.println("                        ");

        System.out.println("Your Birthday...  ");
        System.out.println("Date.." + d1);
        System.out.print("Month.. " + Month);
        System.out.println("  Year.. " + y);

        System.out.println("                        ");

        System.out.println("Your Gender...");
        System.out.println(G);

    }

}


推荐答案

d1未在else块中初始化,仅在if或else if块中初始化,因此如果if()和else if()块中的所有语句都为false,则控制器将转到else块,其中d1仍未初始化。因此,当您尝试在system.out.println()中访问它时,编译器正确地抛出错误。您可能希望在开头将值初始化为0.

d1 is not initialized in the else block and in only in the if or else if blocks, so if all the statements in if() and else if() blocks are false then the controller comes to the else block, where d1 is still not initialized. so when you try to access it inside system.out.println(), the compiler rightly throws the error. you may want to initialize the value to 0 in the beginning.

这篇关于任何人都可以告诉我为什么它给我变量未初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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