jeudi 29 janvier 2015

Can anyone show me why does it give me variable not initialized?

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);

}

}

Aucun commentaire:

Enregistrer un commentaire