sách gpt4 ai đã đi

java - 如何在 Java Mad Libs 程序中使用随机发生器?

In lại 作者:行者123 更新时间:2023-12-01 10:37:29 28 4
mua khóa gpt4 Nike

我正在尝试做 Mad Libs,但我很难弄清楚如何让随机发生器从我创建的数组中选择一个随机动词、名词或形容词,因为它们都是单词,而不是数字...字符串句子也是如此。

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


public class Client {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
Random r = new Random();

//user will enter 5 nouns
System.out.println("Enter 5 nouns");
String[] nouns = { "noun", "noun", "noun", "noun",
"noun"};
for (int ncount = 0; ncount < 5; ncount++) {
System.out.print(nouns[ncount] + ": ");
nouns[ncount] = input.nextLine();
}



//user will enter 5 verbs
System.out.println("Enter 5 verbs");
String[] verbs = { "verb", "verb", "verb", "verb",
"noun"};
for (int vcount = 0; vcount < 5; vcount++) {
System.out.print(verbs[vcount] + ": ");
verbs[vcount] = input.nextLine();
}


//users will enter 5 adjectives
System.out.println("Enter 5 adjectives");
String[] adjectives = { "adjective", "adjective", "adjective", "adjective",
"adjective"};
for (int acount = 0; acount < 5; acount++) {
System.out.print(adjectives[acount] + ": ");
adjectives[acount] = input.nextLine();
}





String sentence1 = "Longwood" + r.verbs.length + "the" + r.adjectives.length + r.nouns.length;
String sentence2 = "The" + noun + "in cafe3" + verb + adjective;
String sentence3 = "The girls will" + verb + adjective + "dresses to the" + noun;
String sentence4 = "Students will" + verb + adjective + "at the" + noun;
String sentence5 = "Students get"+ adjective + "when their" + noun + "are" + "by the staff.";
String sentence6 = "Recieving a day of" + noun+ verb + "me" + adjective;
String sentence7 = "At freshman orientation students" + verb + "about the" + adjective + noun + "on"
+ "the roof";
String sentence8 = "Senior" + noun + "is the place where you can" + verb + adjective +"!";
String sentence9 = "The Longwood Varsity"+ noun + "team" + verb + "Long Island" + adjective + "For 2015!";
String sentence10 = "The teacher of my" + noun + "class" + verb + adjective;

1 Câu trả lời

您可以通过以下方式创建没有任何值的空白数组

String[] array = new String[x]; //x is the size of the array

要选择随机单词,您可以使用 Random 的 nextInt具有数组大小的界限。您获得的随机数将作为要使用的单词的索引

关于java - 如何在 Java Mad Libs 程序中使用随机发生器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34584073/

28 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com