我有一个getRandom方法,该方法从数组中获取一个随机整数并将其返回,但是以某种方式尝试在驱动程序中调用该方法时出现错误
“欧洲轮盘赌类型的方法getRandom(int [])不适用于参数()”
我将如何进行这项工作?我只是试图显示由getRandom方法生成的int。
public final int[] europeanroulette = {0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26};
// random number generated from the roulette
public int getRandom(int[] europeanroulette){
int winningnumber = new Random().nextInt(europeanroulette.length);
return winningnumber;
}
这是从我的司机那里来的:
int winningnumber = europeanroulette.getRandom();
System.out.println("The winning number is" + winningnumber );
最佳答案
与其在包含getRandom()的类中声明int [],不如在驱动程序中声明它,然后将参数传递给方法:
// random number generated from the roulette
public static int getRandom(int[] nums){
int winningnumber = nums[new Random().nextInt(nums.length)]; //using the number as index
return winningnumber;
}
在驱动程序中:
public final int[] europeanroulette= {0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26};
int winningnumber = getRandom(europeanroulette);// or whatever class.getRandom(); (the class where your method is located in.)
System.out.println("The winning number is" + winningnumber );