关于Java的Math.random()怎么使用?

有一道题,希望大家帮我做一下~谢谢了!Theprogramshouldsimulatethefollowingconversation:User:KnockKnock!W... 有一道题,希望大家帮我做一下~谢谢了!
The program should simulate the following conversation:

User: Knock Knock! Who’s There?
Computer: Dewey
User: Dewey who?
Computer: Dewey has to listen to all this knocking?

When the user asks the first question, the first response will be generated randomly by the compute. The second response for the second question will be based on the first response. The responses will be based on the table below:

Response to Who’s There? Next Response
Dewey Dewey has to listen all this knocking?
General Lee Generally I do not tell jokes, okay?
Ken Ken we not have some better jokes?
Wanda Wanda where I put my keys!
这道题就是我问问题,然后电脑随机回复。
展开
 我来答
Jseven_jy
推荐于2016-10-08 · TA获得超过8122个赞
知道大有可为答主
回答量:2499
采纳率:100%
帮助的人:1105万
展开全部
幸亏有点英语水平
意思就是如果第一个问题的回答是General Lee 第二个就必须是Generally I do not tell jokes, okay?其他一样 但是第一个问题的回答是随机的

public class Computer {

private static final String[] names = {"Dewey", "General Lee", "Ken", "Wanda"};
private static final String[] answers = {
"Dewey has to listen all this knocking?",
"Generally I do not tell jokes, okay?",
"Ken we not have some better jokes?",
"Wanda where I put my keys!"};

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true) {
System.out.print("User:");
String str = in.next();
if (str.equals("quit")) {
System.out.println("Bye!");
System.exit(0);
}
System.out.print("Computer:");
int number = (int) (Math.random() * 4);
System.out.println(names[number]);
System.out.print("User:");
str = in.next();
if (str.equals("quit")) {
System.out.println("Bye!");
System.exit(0);
}
System.out.print("Computer:");
System.out.println(answers[number]);
}
}
}
TableDI
2024-07-18 广告
VLOOKUP是Excel中用于垂直查找的函数,其基本用法包括四个参数:1. 查找值:即在数据表首列中需要搜索的值。2. 数据表:包含查找值的单元格区域或数组。3. 返回值所在列数:指定返回查询区域中第几列的值。4. 查找方式:选择精确匹配... 点击进入详情页
本回答由TableDI提供
百度网友e829334c0
2010-06-28 · TA获得超过473个赞
知道小有建树答主
回答量:1066
采纳率:0%
帮助的人:827万
展开全部
产生一个(0,1]的随机数,直接赋值给变量
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式