我正在尝试从数组列表中随机选择 5 个元素。由于某种原因,它总是选择 0 作为随机元素。有什么帮助吗??这是我到目前为止的代码。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
public class Rewards {
public static void main(String[] args) throws FileNotFoundException {
String fileName = ("C:/Users/Jordan/Desktop/Proj5.txt");
FileReader fin = new FileReader(fileName);
Scanner src = new Scanner(fin);
ArrayList<String> lines = new ArrayList<String>();
src.useDelimiter(":");
while (src.hasNext()) {
lines.add(src.nextLine());
System.out.println(src.next());
// replace above line with array
}
String[] lineArray = new String[lines.size()];
lines.toArray(lineArray);
Random rand = new Random();
for (int i = 0; i < 5; i++) {
int choice = rand.nextInt(lineArray.length);
System.out.println("Random Winner is: " + choice);
}
}
}