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();
    }
}

}

用随机数生成英文句子:Java 代码示例

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

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