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

public class RandomSentenceGenerator {

// 定义四个数组,分别存储冠词、名词、动词、介词
private static String[] article = { 'the', 'a', 'one', 'some', 'any' };
private static String[] noun = { 'boy', 'girl', 'dog', 'town', 'cat' };
private static String[] verb = { 'drove', 'jumped', 'ran', 'walked', 'skipped' };
private static String[] preposition = { 'to', 'from', 'over', 'under', 'on' };

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    Random random = new Random();

    while (true) { // 无限循环,直到用户输入exit
        // 生成随机数,用于在4个数组中随机取出6个单词,按 (article,noun,verb,preposition,article,noun)顺序组成句子
        int index1 = random.nextInt(article.length);
        int index2 = random.nextInt(noun.length);
        int index3 = random.nextInt(verb.length);
        int index4 = random.nextInt(preposition.length);
        int index5 = random.nextInt(article.length);
        int index6 = random.nextInt(noun.length);

        // 组成句子
        String sentence = article[index1] + ' ' + noun[index2] + ' ' + verb[index3] + ' ' + preposition[index4] + ' ' + article[index5] + ' ' + noun[index6];

        // 将句子首字母大写
        sentence = sentence.substring(0, 1).toUpperCase() + sentence.substring(1);

        // 输出句子,并以圆点结尾
        System.out.println(sentence + '.');

        // 如果用户输入exit,则退出循环
        if (scanner.nextLine().equals('exit')) {
            break;
        }
    }
}

}

随机句子生成器:使用随机数创建简单句子

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

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