2016-10-14 42 views
0

我必须做一个星座运算,在这个过程中,我选择声明一个字符串,然后在if声明中设置它相等。我似乎无法在java中声明这个变量

我在最后一行发生错误,指出s & t未初始化。我错过了很简单的事情吗?任何帮助是极大的赞赏。

import java.util.Scanner; 

class main { 
    public static void main(String[] args) 
    { 

    Scanner scan = new Scanner(System.in); 

    System.out.println("What day of the month were you born? (number)"); 
    int d = scan.nextInt(); 
    System.out.println("Which month were you born? (number)"); 
    int m = scan.nextInt(); 

    if (m==3 && d>=21 && d<=31) 
     System.out.println("Your sign is Aries"); 
    else if (m==4 && d<=19 && d>=1) 
     System.out.println("Your sign is Aries"); 
    else if (m==4 && d>=20 && d<=31) 
     System.out.println("Your sign is Taurus"); 
    else if (m==5 && d<=20 && d>=1) 
     System.out.println("Your sign is Taurus"); 
    else if (m==5 && d>=21 && d<=31) 
     System.out.println("Your sign is Gemini"); 
    else if (m==6 && d>=1 && d<=20) 
     System.out.println("Your sign is Gemini"); 
    else if (m==6 && d>=21 && d<=31) 
     System.out.println("Your sign is Cancer"); 
    else if (m==7 && d>=1 && d<=22) 
     System.out.println("Your sign is Cancer"); 
    else if (m==7 && d>=23 && d<=31) 
     System.out.println("Your sign is Leo"); 
    else if (m==8 && d>=1 && d<=22) 
     System.out.println("Your sign is Leo"); 
    else if (m==8 && d>=23 && d<=31) 
     System.out.println("Your sign is Virgo"); 
    else if (m==9 && d>=1 && d<=22) 
     System.out.println("Your sign is Virgo"); 
    else if (m==9 && d>=23 && d<=31) 
     System.out.println("Your sign is Libra"); 
    else if (m==10 && d>=1 && d<=22) 
     System.out.println("Your sign is Libra"); 
    else if (m==10 && d>=23 && d<=31) 
     System.out.println("Your sign is Scorpio"); 
    else if (m==11 && d>=1 && d<=21) 
     System.out.println("Your sign is Scorpio"); 
    else if (m==11 && d>=22 && d<=31) 
     System.out.println("Your sign is Sagittarius"); 
    else if (m==12 && d>=1 && d<=21) 
     System.out.println("Your sign is Sagittarius"); 
    else if (m==12 && d>=22 && d<=31) 
     System.out.println("Your sign is Capricorn"); 
    else if (m==1 && d>=1 && d<=19) 
     System.out.println("Your sign is Capricorn"); 
    else if (m==1 && d>=20 && d<=31) 
     System.out.println("Your sign is Aquarius"); 
    else if (m==2 && d>=1 && d<=18) 
     System.out.println("Your sign is Aquarius"); 
    else if (m==2 && d>=19 && d<=31) 
     System.out.println("Your sign is Pisces"); 
    else if (m==3 && d>=1 && d<=20) 
     System.out.println("Your sign is Pisces"); 
    else 
     System.out.println("error"); 

    String s; 

    if (m==1) 
     s=("January"); 
    else if (m==2) 
     s=("February"); 
    else if (m==3) 
     s=("March"); 
    else if (m==4) 
     s=("April"); 
    else if (m==5) 
     s=("May"); 
    else if (m==6) 
     s=("June"); 
    else if (m==7) 
     s=("July"); 
    else if (m==8) 
     s=("August"); 
    else if (m==9) 
     s=("September"); 
    else if (m==10) 
     s=("October"); 
    else if (m==11) 
     s=("November"); 
    else if (m==12) 
     s=("December"); 

    String t; 

    if (d==1) 
     t=("first"); 
    else if (d==2) 
     t=("second"); 
    else if (d==3) 
     t=("third"); 
    else if (d==4) 
     t=("fourth"); 
    else if (d==5) 
     t=("fifth"); 
    else if (d==6) 
     t=("sixth"); 
    else if (d==7) 
     t=("seventh"); 
    else if (d==8) 
     t=("eighth"); 
    else if (d==9) 
     t=("ninth"); 
    else if (d==10) 
     t=("tenth"); 
    else if (d==11) 
     t=("eleventh"); 
    else if (d==12) 
     t=("twelfth"); 
    else if (d==13) 
     t=("thirteenth"); 
    else if (d==14) 
     t=("fourteenth"); 
    else if (d==15) 
     t=("fifteenth"); 
    else if (d==16) 
     t=("sixteenth"); 
    else if (d==17) 
     t=("seventeenth"); 
    else if (d==18) 
     t=("eighteenth"); 
    else if (d==19) 
     t=("nineteenth"); 
    else if (d==20) 
     t=("twentieth"); 
    else if (d==21) 
     t=("twenty-first"); 
    else if (d==22) 
     t=("twenty-second"); 
    else if (d==23) 
     t=("twenty-third"); 
    else if (d==24) 
     t=("twenty-fourth"); 
    else if (d==25) 
     t=("twenty-fifth"); 
    else if (d==26) 
     t=("twenty-sixth"); 
    else if (d==27) 
     t=("twenty-seventh"); 
    else if (d==28) 
     t=("twenty-eighth"); 
    else if (d==29) 
     t=("twenty-ninth"); 
    else if (d==30) 
     t=("thirtieth"); 
    else if (d==31) 
     t=("thirty-first"); 

    System.out.println("Your birthday is: " + s + " " + t); 

    } 
} 
+1

如果没有一个条件是真的会怎么样?那么将会成为什么呢?发布你的代码*在这里*,阅读错误,并摆脱那些多余的括号。 – Li357

+1

不要做pastebin! – HSchmale

+0

把代码放在问题中。有人会为你清理它,如果它不漂亮 – Andreas

回答

0

你的问题来自你的长字符串if-elseif的代码。当你有

else if (d==31) 
    t=("thirty-first"); 

应该

else 
    t = ("thirty-first"); 

,类似的还有秒。如果你的变量只是在条件语句中初始化的,Java总是会抱怨,因为它有可能永远不会被初始化。您也可以使用

String s = ""; 

来避免这种情况。

+0

啊,这是你提到的第二件事,谢谢一堆! –

+0

和我提交它没有做你最先提到的。如果这个数字不是真实的日子,我不需要输出一个错误信息,这样做不行吗? –

+0

所以如果我输入42作为我出生的那天,程序会告诉我我出生在第三十一位? – Tobb

-1

您是否期望字符串像原始图一样工作?

public static void main(String ... args){ 
    int myInt; //implicitly initialized to 0 
    String myString; //not implicitly initialized; objects don't have default values 
    myInt=myInt+1; //works! myInt now is one 
    myString=yString+"test"; //compilier error, myString was never initiallized 
} 

编辑,看到了张贴代码:

注意,编译器是足够聪明,知道有你的代码的方式,其中变量未初始化:

public static void main(String ... args){ 
    String myString; //not implicitly initialized 
    Scanner scan = new Scanner(System.in); 
    int d = scan.nextInt(); 
    if(d>100){ 
    myString="hello big world!"; 
    }else if(d<10){ 
    myString="hello little world!"; 
    } 
    System.out.println(myString); //error, the compiler knows that myString is uninitialized in some cases. 
} 

请尝试类似这样的方式:

public static void main(String ... args){ 
    String myString; //not implicitly initialized 
    Scanner scan = new Scanner(System.in); 
    int d = scan.nextInt(); 
    if(d>100){ 
    myString="hello big world!"; 
    }else if(d<10){ 
    myString="hello little world!"; 
    }else{ 
    myString="hello world!"; 
    } 
    System.out.println(myString); //works! the compiler knows that there is no way through that if block without myString getting set. 
} 
+1

关于答案中的第一段,变量(不管它们是基元还是对象引用)在它们是方法中的局部变量时未被初始化。当它们是对象的字段时,它们始终被初始化。 –

0

一些一般的指针有关代码:

围绕字符串文字不需要paranthesis。 ("first")可以替换为"first"

长链if-else很难阅读,并认为不好。 A switch会更好,但我实际上至少会用Map来解决日期和月份。

你的代码应该分解成方法,每个方法负责一个“事物”。这里你至少有4件事情:(1)日月的输入,(2)符号的计算,(3)编号的月份到文本的转换,以及(4)编号的日期到文本的转换。

作为一个例子,在这里为方法(3):

private String getMonthAsString(final Integer monthAsNumber) { 
    return createMonthNumberToMonthStringMap().get(monthAsNumber); 
} 

private Map<Integer, String> createMonthNumberToMonthStringMap() { 
    final Map<Integer, String> monthNumberToMonthString = new HashMap<>(); 
    monthNumberToMonthString.put(1, "January"); 
    monthNumberToMonthString.put(2, "February"); 
    monthNumberToMonthString.put(3, "March"); 
    monthNumberToMonthString.put(4, "April"); 
    monthNumberToMonthString.put(5, "May"); 
    monthNumberToMonthString.put(6, "June"); 
    monthNumberToMonthString.put(7, "July"); 
    monthNumberToMonthString.put(8, "August"); 
    monthNumberToMonthString.put(9, "September"); 
    monthNumberToMonthString.put(10, "October"); 
    monthNumberToMonthString.put(11, "November"); 
    monthNumberToMonthString.put(12, "Desember"); 

    return monthNumberToMonthString; 
} 

如果一个无效的号码(-1,0,13,等等)被发送到getMonthAsString它将返回null。您应该在这些情况下抛出异常,或向用户提供详细说明问题的错误消息。