2016-11-18 283 views
0

我正在嘗試編寫一個程序,詢問用戶他們的寵物名稱是什麼,物種,找到口渴級別並相應地給出響應。Java以一種方式返回多個字符串

我將不勝感激,如果有人可以幫助我有一個問題,IM,在每個2種方法askpetname和thirstlevel有2串我想整個類訪問,而無需使用全局變量。

有人能告訴我,這是我在做什麼錯誤或點我在正確的方向。

而且,據我所知,我的過度使用的繁瑣任務的方法是不好的做法,但它與記憶語法幫助。

謝謝。

class dinoo 
{ 
public static void main(String[] p) 
{ 


    explain(); 
    output(); 

    System.exit(0); 

}

public static void explain() 
{ 

    print("The following program demonstrates use of user input by  asking for pet name."); 

    return; 
} 

public static String askpetname() 
{ 
    Scanner scanner = new Scanner(System.in); 

    print("Name your dinosaur pet!"); 

    String petname = scanner.nextLine(); 

    print("Awesome, cool dinosaur name, what species is " + petname+ " ?"); 

    String petspecies = scanner.nextLine(); 

    return petname, petspecies; 
} 

public static int thirstlevel() 
{ 
    Random ran = new Random(); 

    int thirst = ran.nextInt(11); 
    int hunger = ran.nextInt(11); 


    return thirst,hunger; 
} 

public static String anger(int thirst, int hunger) 
{ 
    double angerscore = (thirst+hunger)/2; 
    String temper;  

    if(angerscore<=2) 
    { 
     temper = "Serene"; 
    } 

    else if(3<=angerscore<=6) 
    { 
     temper= "Grouchy"; 
    } 

    else if(6<angerscore) 
    { 
     temper = "DANGEROUS"; 
    } 

    return temper; 
} 


public static String warning() 
{ 
    if (temper.equals("Serene")) 
    { 
     print("He's looking happy!"); 
    } 
    else if(temper.equals("Grouchy")) 
    { 
     print("Ahhh hes a bit "+temper+", you better start feeding him before he gets mad!"); 
    } 

    else if(temper.equals("DANGEROUS")) 
    { 
     print("GET OUT OF THERE, HES " + temper+"!!!. He will have to be put down for everyones safety."); 
    } 

} 
public static void output() 
{ 
    print(askpetname() + "'s, thirst level is "+thirstlevel()+"/10"); 

    return; 
} 



public static String print(String message) 
{ 
    System.out.println(message); 

    return message; 
} 

}

+0

你能澄清你正在談論哪些字符串嗎?看着'thirstlevel()'裏面沒有字符串?你還會在哪裏使用這些字符串?全球變量並不是世界上最糟糕的事情。 – user123

+0

我的壞我的意思是int在thirstlevel。 – coding95

回答

3

該代碼將無法編譯,因爲你沒有可以:

return string1, string2; 

else if(3<=angerscore<=6) 

而不是試圖返回多個字符串,最好的辦法是創建一個類,說叫Pet,一個持有字符串字段的寵物的名字,它的物種種類領域,以及其他領域的飢渴......這將最好地封裝構成一個邏輯「寵物」的所有數據以及諸如getAnger()的方法,該方法根據寵物的狀態返回憤怒的值。然後,您可以從創建方法創建並返回可用的Pet對象。

而且,你的代碼有許多編譯錯誤的,建議你可以改善你創建你的代碼的方式。切勿嘗試將新代碼添加到「不良」代碼中,添加到無法編譯的代碼中。如有可能,請使用NetBeans,Eclipse或IntelliJ等IDE來幫助您創建程序。如果您的任何代碼包含編譯錯誤,則IDE將標記您,然後鍵爲:在您首先解決了現有編譯錯誤之前不會添加新代碼。如果你不能使用IDE,那麼你必須儘早且經常編譯,並且做同樣的事情 - 在添加新的之前修正所有的錯誤。

+0

我很欣賞這個反饋。 – coding95

0

首先,我會建議在嘗試此操作之前先通過教程拍攝,然後執行涵蓋範圍,對象,數組和函數的所有問候世界。熟悉面向對象的風格,儘管這甚至不是程序編程...沒有任何東西返回2個對象...總是1(它可能是一個包含許多對象的數組,但是一個數組是單個對象)

繼續前進,雖然這是很可怕的編碼習慣,但其確定的初學者,因爲你的函數都是靜態的,創建的每個函數內部的私有靜態變量,創建getter函數

//convert 
String petname = scanner.nextLine(); 
// To this 
private static String petname = scanner.nextLine(); 
// Then add this below it 
public static String getPetName() 
{ 
return petname; 
} 

和同爲每一塊數據的需要。

現在從您的所有功能刪除return語句,並聲明返回類型爲void

然後調用所有功能從主,

askpetname(); 
thirstlevel(); 

然後打印最終輸出(您已調用的函數後)因此

System.out.println("Petname: " + getPetname + " ThirstLevel: " + getThirstLevel() + " HungerLevel: " + getHungerLevel); 
+0

我明白了,生病了。感謝您的意見。 – coding95

+0

歡迎您,但我認真地向您展示瞭如何使其工作,快速和骯髒,最好的建議是通過教程並相應地進行調整 –