2017-10-20 88 views
-1

這是我的代碼到目前爲止,我怎麼得到這個工作,因爲我努力打印一個句子,更不用說隨便了。如何獲取此代碼從每個數組中選擇一個隨機單詞來打印一個句子?

我需要從每個陣列一個字的陣列

import java.util.Random; 

public class MadLibs { 

    public static void main(String[] args) { 

     String[] nouns = null; 
     String[] verbs = null; 
     String[] adjec = null; 
     String[] adver = null; 

     nouns[0] = "Lion"; 
     nouns[1] = "Stewart"; 
     nouns[2] = "Timbaktu"; 
     nouns[3] = "Kilamanjaro"; 
     nouns[4] = "Hawai"; 
     nouns[5] = "Frog"; 
     nouns[6] = "Russia"; 
     nouns[7] = "Dimethyltryptaline"; 
     nouns[8] = "Dog"; 
     nouns[9] = "Badness"; 

     verbs[0] = "hear"; 
     verbs[1] = "achieve"; 
     verbs[2] = "smash"; 
     verbs[3] = "destroy";     
     verbs[4] = "smoke"; 
     verbs[5] = "burn"; 
     verbs[6] = "rose"; 
     verbs[7] = "speak"; 
     verbs[8] = "type"; 
     verbs[9] = "be"; 

     adjec[0] = "Tangy"; 
     adjec[1] = "Sour"; 
     adjec[2] = "Smooth"; 
     adjec[3] = "Sticky"; 
     adjec[4] = "Soft"; 
     adjec[5] = "Black"; 
     adjec[6] = "Whispering"; 
     adjec[7] = "Resonant"; 
     adjec[8] = "Wide"; 
     adjec[9] = "Long"; 

     adver[0] = "Truthfully"; 
     adver[1] = "Endlessly"; 
     adver[2] = "Painfully"; 
     adver[3] = "Tirelessly"; 
     adver[4] = "Beautifully"; 
     adver[5] = "Uneasily"; 
     adver[6] = "Wickedly"; 
     adver[7] = "Briskly"; 
     adver[8] = "Now"; 
     adver[9] = "Last"; 


     String random = (verbs[new Random().nextInt(verbs.length)]); 


     System.out.println("Does your" + verbs + " your " + adjec + nouns + adver); 


    } 

} 
+0

'Math.random'或'Random' – Korashen

+2

你應該先初始化數組 - '的String []名詞=新的String [10];'。 –

回答

-1

在這裏你去:)希望這是你想要的使用的話打印句子(通常我不會解決學校裏的功課,但有時你只是覺得它):

public static void main(String[] args) { 

    String[] nouns = new String[10]; 
    String[] verbs = new String[10]; 
    String[] adjec = new String[10]; 
    String[] adver = new String[10]; 

    nouns[0] = "Lion"; 
    nouns[1] = "Stewart"; 
    nouns[2] = "Timbaktu"; 
    nouns[3] = "Kilamanjaro"; 
    nouns[4] = "Hawai"; 
    nouns[5] = "Frog"; 
    nouns[6] = "Russia"; 
    nouns[7] = "Dimethyltryptaline"; 
    nouns[8] = "Dog"; 
    nouns[9] = "Badness"; 

    verbs[0] = "hear"; 
    verbs[1] = "achieve"; 
    verbs[2] = "smash"; 
    verbs[3] = "destroy";     
    verbs[4] = "smoke"; 
    verbs[5] = "burn"; 
    verbs[6] = "rose"; 
    verbs[7] = "speak"; 
    verbs[8] = "type"; 
    verbs[9] = "be"; 

    adjec[0] = "Tangy"; 
    adjec[1] = "Sour"; 
    adjec[2] = "Smooth"; 
    adjec[3] = "Sticky"; 
    adjec[4] = "Soft"; 
    adjec[5] = "Black"; 
    adjec[6] = "Whispering"; 
    adjec[7] = "Resonant"; 
    adjec[8] = "Wide"; 
    adjec[9] = "Long"; 

    adver[0] = "Truthfully"; 
    adver[1] = "Endlessly"; 
    adver[2] = "Painfully"; 
    adver[3] = "Tirelessly"; 
    adver[4] = "Beautifully"; 
    adver[5] = "Uneasily"; 
    adver[6] = "Wickedly"; 
    adver[7] = "Briskly"; 
    adver[8] = "Now"; 
    adver[9] = "Last"; 

    String v = (verbs[new Random().nextInt(verbs.length)]); 
    String a = (adjec[new Random().nextInt(adjec.length)]); 
    String n = (nouns[new Random().nextInt(nouns.length)]); 
    String ad = (adver[new Random().nextInt(adver.length)]); 

    System.out.println("Does your " + v + " your " + a + " " + n + " " + ad); 
} 

但是,句子沒有意義。這句話或許應該進來像另一個順序:

System.out.println("Does your " + a + " " + n + " " + v + " " + ad); 
相關問題