import java.util.Random; import java.util.Scanner;

public class RandomSentence { public static void main(String[] args) { String[] article = {"the", "a", "one", "some", "any"}; String[] noun = {"boy", "girl", "dog", "town", "cat"}; String[] verb = {"drove", "jumped", "ran", "walked", "skipped"}; String[] preposition = {"to", "from", "over", "under", "on"};

    Scanner scanner = new Scanner(System.in);
    Random random = new Random();

    while (true) {
        String sentence = "";
        for (int i = 0; i < 6; i++) {
            switch (i) {
                case 0:
                case 4:
                    sentence += article[random.nextInt(article.length)] + " ";
                    break;
                case 1:
                case 5:
                    sentence += noun[random.nextInt(noun.length)] + " ";
                    break;
                case 2:
                    sentence += verb[random.nextInt(verb.length)] + " ";
                    break;
                case 3:
                    sentence += preposition[random.nextInt(preposition.length)] + " ";
                    break;
            }
        }
        sentence = sentence.substring(0, 1).toUpperCase() + sentence.substring(1) + ".";
        System.out.println(sentence);
        scanner.nextLine();
    }
}

}

用随机数生成语句编写一个程序用随机数生成语句。该程序使用4个String类型的数组它们分别是article冠词noun名词verb动词preposition介词。数组内容如下:article数组包含冠词:theaonesomeanynoun数组包含名词boygirldogtowncatverb数组包含动词drovejumpedranwalkedskippedpreposition数组应包含介词to

原文地址: https://www.cveoy.top/t/topic/bRMw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录