用JAVA编写吃饭程序

来源:百度知道 编辑:UC知道 时间:2024/05/14 12:28:47
Math.random()方法编写一个去哪里吃饭的程序

Math.random()过时啦,用Random类吧.专用的随机数生成类
可以方便的生成你需要范围内的数值.
比如nextInt(),nextFloat()这些经典方法.

public static void main(String[] args) {
Random rnd = new Random(System.currentTimeMillis());
String[] rest = {"力天餐厅","北京饭店","饿着","全聚德","楼下摊煎饼"};
int select = rnd.nextInt(rest.length);
System.out.println(rest[select]);
}