这是我正在参加的java课程吗?它应该生成一些随机索引,并要求你在数组的索引处放置一个新的整数。
在ChangeOne()方法的末尾使用console.close()有一个奇怪的问题。我知道完成它后关闭Scanner实例的标准,所以在这种情况下我不明白为什么不适用:
import java.util.*;
public class ChangeUp {
public static void main(String[] args)
{
int[] list = new int[6];
printArray(list);
for (int x=0; x<6; x++)
{
changeOne(list);
printArray(list);
}
}
public static void printArray(int[] somearray)
{
int arraylen = somearray.length;
for(int j=0; j < arraylen; j++)
{
System.out.println("Value: " + somearray[j]);
}
}
public static void changeOne(int[] somearray)
{
int arraylen = somearray.length;
Random r = new Random();
int index = r.nextInt(arraylen);
Scanner console = new Scanner(System.in);
System.out.printf("The chosen index equals %d. Enter a new integer for that index: \n", index);
while ( !console.hasNextInt())
{
console.next();
System.out.printf("The chosen index equals %d. Enter a new integer for that index: \n", index);
}
int v = console.nextInt();
somearray[index] = v;
console.close();
}
}