import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class LuckyAudience { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<String> audienceList = new ArrayList<String>(); System.out.println("请输入3名观众的姓名:"); for (int i = 0; i < 3; i++) { String name = scanner.next(); audienceList.add(name); } System.out.println("观众姓名总览:"); for (String name : audienceList) { System.out.println(name); } Random random = new Random(); int luckyIndex = random.nextInt(3); String luckyAudience = audienceList.get(luckyIndex); System.out.println("幸运观众是:" + luckyAudience); } }import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class LuckyAudience { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<String> audienceList = new ArrayList<String>(); System.out.println("请输入3名观众的姓名:"); for (int i = 0; i < 3; i++) { String name = scanner.next(); audienceList.add(name); } System.out.println("观众姓名总览:"); for (String name : audienceList) { System.out.println(name); } Random random = new Random(); int luckyIndex = random.nextInt(3); String luckyAudience = audienceList.get(luckyIndex); System.out.println("幸运观众是:" + luckyAudience); } }
时间: 2023-05-17 12:05:33 浏览: 108
Java_class_CHINESE.rar_class_java chm_java 类库
这是一个 Java 程序,用于从输入的观众姓名中随机选出一位幸运观众。程序首先创建一个 Scanner 对象,然后通过 for 循环输入 3 名观众的姓名,并将它们添加到 ArrayList 中。接着,程序使用 for-each 循环输出观众姓名总览。最后,程序使用 Random 类生成一个随机数,选出幸运观众,并输出其姓名。
阅读全文