2016-10-14 41 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。您應該在這些情況下拋出異常,或向用戶提供詳細說明問題的錯誤消息。