帮我写一个关于晚上吃什么的Java代码
时间: 2023-02-08 19:59:57 浏览: 131
当然可以!下面是一个简单的例子,可以随机地选择晚上吃什么:
```
import java.util.Random;
public class WhatToEat {
public static void main(String[] args) {
String[] options = {"炸鸡", "披萨", "寿司", "意大利面", "烤肉"};
Random rand = new Random();
int index = rand.nextInt(options.length);
System.out.println("晚上吃:" + options[index]);
}
}
```
这段代码定义了一个字符串数组 `options`,里面包含了一些可供选择的晚餐食物,然后使用 `java.util.Random` 类的 `nextInt()` 方法生成一个随机的下标,最后通过下标来从数组中获取对应的食物名称,并打印到控制台上。
你可以按照自己的喜好来修改食物列表,以及针对不同场景做调整。
相关问题
用java写一个晚上吃什么
好的,以下是一个简单的Java代码,用来随机生成一个晚餐的菜单:
```java
import java.util.Random;
public class DinnerMenu {
public static void main(String[] args) {
// 菜单选项
String[] menu = {"炒面", "炒饭", "牛肉面", "麻辣烫", "煎饼果子", "炸鸡", "炒菜", "火锅", "寿司", "披萨"};
// 随机生成菜单
Random random = new Random();
int index = random.nextInt(menu.length);
String dinner = menu[index];
// 输出菜单
System.out.println("今晚吃 " + dinner);
}
}
```
这个代码会在菜单选项中随机选择一个菜品作为晚餐,然后输出选择的菜品。你可以根据自己的需要修改菜单选项。
用java给我写一个下载多张表格的代码
这是一段用 Java 的代码来下载多张表格的例子:
```
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
public class DownloadTable {
public static void main(String[] args) throws IOException {
String[] tableUrls = {
"https://example.com/table1.xls",
"https://example.com/table2.xls",
"https://example.com/table3.xls"
};
for (String tableUrl : tableUrls) {
downloadTable(tableUrl);
}
}
public static void downloadTable(String tableUrl) throws IOException {
URL url = new URL(tableUrl);
BufferedInputStream bis = new BufferedInputStream(url.openStream());
String fileName = tableUrl.substring(tableUrl.lastIndexOf("/") + 1);
FileOutputStream fos = new FileOutputStream(fileName);
BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
byte[] buffer = new byte[1024];
int count;
while ((count = bis.read(buffer, 0, 1024)) != -1) {
bos.write(buffer, 0, count);
}
bos.close();
fos.close();
bis.close();
}
}
```
这段代码将会下载一个存储在数组 `tableUrls` 中的所有表格。你可以在数组中添加或删除表格的 URL,然后运行这段代码就可以下载多张表格了。
希望这能帮到你!