我想使用数组 comNumber
的元素,在 ranNumber()
方法
但是当我在main()
中使用它时它只显示数组的地址。
我该如何解决这个问题?
public class BaseballGame {
public static void main(String[] args) {
BaseballGame bGame = new BaseballGame();
System.out.println(bGame.ranNumber());
}
public int[] ranNumber() {
Random rand = new Random();
int[] comNumber = new int[3];
for(int i=0; i<comNumber.length; i++) {
comNumber[i] = rand.nextInt(10);
for(int j=0; j<i; j++) {
if (comNumber[i] == comNumber[j]) {
i--;
break;
}
}
}
return comNumber;
}
}
最佳答案
如果您想使用它,请不要打印它。
int[] result = bGame.ranNumber();
现在在您的主方法中使用结果
。
如果您想查看返回的内容,
System.out.println(Arrays.toString(result));
<小时/>
public static void main(String[] args) {
BaseballGame bGame = new BaseballGame();
int[] result = bGame.ranNumber();
System.out.println(Arrays.toString(result));
}
关于java - 如何在 main 中使用数组方法中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48606533/