import java.util.Scanner;
public class ZodiacSign{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int Feb,March,ZodiacSign,Pisces,Aquarius,Aries,Taurus;
int selection;
System.out.println("Feb");
System.out.println("Feb 1,2,3,4\n Feb 5,6,7,8\n Feb 9,10,11,12\n Feb 13,14,15,16\n Feb 17,18,19,20\n Feb 21,22, 23, 24\n Feb25,26,27,28\n March 1,2,3,4\n March 5,6,7,8\n March 9,10,11,12\n March 13,14,15,16\n March17,18,19,20\n March 21,22,23,24\n March 25,26,27,28\n March 29,30,31");
System.out.println("Please enter your date of birth");
System.out.println("Please enter your month of birth");
selection = input.nextInt();
if(Feb>= 19){
ZodiacSign = Pisces ;
System.out.println("Your zodiac sign is Pisces");
}
else
{
System.out.println("Your zodiac sign is Aquarius");
}
if(March>=22){
ZodiacSign = Aries;
System.out.println("Your zodiac sign is Aries");
}
else
{
System.out.println("Your zodiac sign is Taurus");
}
System.out.println("End of Program");
}
}
它給我的四個錯誤:變量可能尚未初始化
ZodiacSign.java:19:error: variable Feb might not have been initialized
if(Feb>= 19){
^
ZodiacSign.java:20:error: variable Pisces might not have been initialized
ZodiacSign= Pisces ;
^
ZodiacSign.java:27:error: variable March might not have been initialized
if(March>= 22){
^
ZodiacSign.java:28:error: variable Aries might not have been initialized
ZodiacSign= Aries ;
^
它們不需要默認值。在閱讀之前,他們必須被明確地分配 - 這不是一回事。 –
是的,我同意。那更精確。我編輯了我的回覆。 – Santosh
雖然我不認爲這是正確的做法。 (我也建議使用「局部變量」而不是「方法變量」這個更常見的術語。) –