我想在循环的每次迭代中获得不同的对象。我从另一个方法中定义的对象数组中获取对象并在此方法中使用,但对象(书)保持不变。 对于每个循环,后面跟着其他语句并正常工作,因此没有相关性。
public static String [] runSimulation(LibraryBook [] bookStock, int numberOFevents){
Random rnd = new Random();
String [] output = new String [numberOFevents];
int a = rnd.nextInt(100);
int n = LibraryBook.getBooks();
int c = rnd.nextInt(1);
int random = rnd.nextInt(n-1);
//for (int index=0; index<numberOFevents; index++) {
int i=0;
for (LibraryBook book : bookStock ){
i++;
book = bookStock[random];
if (book.getLibraryClass() == null) {
book.setLibraryClass("QA" + a );
output[i] += (i + book.getLibraryClass()+ "BOOK IS CLASSIFIED");
}
else if (book.getLibraryClass() != null &&
"REFERENCE_ONLY".equals(book.getBookStatus())){
output[i] += (i + book.getLibraryClass() + "REFERENCE ONLY BOOK");
}
答案 0 :(得分:0)
因为您不是每次都在循环中随机更改。您的代码应如下所示:
int random;
int i=0;
for (LibraryBook book : bookStock ){
i++;
random = rnd.nextInt(n-1); // sets a new value in every loop
book = bookStock[random];