2014-11-21 59 views
0

我被要求創建一個程序,用於存儲數組中的一系列適當的名詞,形容詞和動詞。這些必須在程序運行開始時設置。與其詢問用戶,每次產生字母時,它只是從相應的數組中隨機選擇單詞。數組傳遞給表示模板的方法。在數組中選擇隨機字符串以打印消息

我是新來的java,這是我已經設法在下面完成,但是顯示錯誤說void不能轉換爲打印消息部分的字符串。如果有人能幫助我處理這個我正在努力的簡單問題,我會很高興,我不知道我是否正確地做到了這一點:/。任何幫助,將不勝感激:)

public static void arrays() 
{ 

     final String []noun = {"face", "eyes", "tender", "lips", "ears", "roses"}; 
     Random random = new Random(); 
     int rand1 = random.nextInt(noun.length); 


     final String []verb = {"enchant", "dazzle", "cuddle" , "lure", "desire", "dream" }; 
     Random random2 = new Random(); 
     int rand2 = random2.nextInt(verb.length); 


     final String []adjective = { "Alluring", "Angelic", "Adoring", "Appealing", "Attractive", "beautiful"}; 
     Random random3 = new Random(); 
     int rand3 = random3.nextInt(adjective.length); 


     String message = printmessage (noun[rand1], verb[rand2], adjective[rand3]); 

     JOptionPane.showMessageDialog(null, message); 

} 
    // END arrays 

    public static String printmessage(String r1, String r2, String r3) 
    { 
     String result1; 
     String result2; 
     String result3; 
     String result4; 

     result1 = JOptionPane.showMessageDialog(null, "I would love to " + r2 + " " + r3 + " " + r1 + "\n"); 
     return result1; 
     result2 = JOptionPane.showMessageDialog(null, "Your are my " + r1 + " " + r3 + " " + r2 + "\n"); 
     return result2; 
     result3 = JOptionPane.showMessageDialog(null, "you always look great in that " + r1 + " , as you always do, since your so" + r3 + "\n"); 
     return result3; 
     result4 = JOptionPane.showMessageDialog(null, "I get butterflies when I see you in" + r1 + " , you make me " + r2 + " , in your " + r3 + " world" + "\n"); 
     return result4; 

    } 
+7

嗯,非常浪漫的代碼有 – Coffee 2014-11-21 17:06:04

+2

我的猜測是你不是在主要方法中編寫代碼......而是寫在課堂上。顯示更多的代碼,所以我們可以看到有什麼問題.. – user1071777 2014-11-21 17:07:27

+0

這就是說,所有問題都說明了,代碼還有什麼? @ user1071777 – Arpit 2014-11-21 17:10:34

回答

2

在此行中:

String message = printmessage (rand1, rand2, rand3); 

你應該通過String而是要傳遞int

改變它是這樣的:

String message = printmessage (noun[rand1], verb[rand2], adjective[rand3]); 

編輯:

此外,因爲您要創建的字符串message不要刪除return但改變方法:

public static String printmessage(String r1, String r2, String r3) 

編輯編輯:

您將需要更新您的printmessage功能實際創建一個String,類似於String printmessage = "I would love to " + r2 + " " + r3 + " " + r1 + "\n"

+0

啊對,我完全按照你的說法做了,它在返回printmessage語句中顯示另外1個錯誤,表示「無法找到符號」,我想如何解決這個問題:/ @Daniel Stanley – Arpit 2014-11-21 17:23:53

+0

@Arpit在返回之前,您仍然需要創建該字符串,請參閱「EDIT EDIT」 – 2014-11-21 17:28:26

+0

我改變了它,它提出了更多的錯誤,說不兼容的類型再次,void不能轉換爲字符串,@丹尼爾斯坦利 – Arpit 2014-11-21 17:35:54