Java 使用int变量进行列表放置
我不确定我的措辞是否正确。我希望这会更清楚Java 使用int变量进行列表放置,java,list,Java,List,我不确定我的措辞是否正确。我希望这会更清楚 Integer[] Performed = new Integer[3]; public String dived(){ while (this.numberAttempts<3){ int n = this.numberAttempts; int k = Dive.chosenRandomly(); this.Performed[n]
Integer[] Performed = new Integer[3];
public String dived(){
while (this.numberAttempts<3){
int n = this.numberAttempts;
int k = Dive.chosenRandomly();
this.Performed[n] = k ;
numberAttempts += 1;
}
return null;
}
Integer[]已执行=新整数[3];
公共字符串dived(){
而(this.numberAttempts只需使用一些列表,例如ArrayList,并向其中添加随机整数即可
List<Integer> Performed = new ArrayList<Integer>();
public String dived(){
while (this.numberAttempts<3){
int n = this.numberAttempts;
int k = Dive.chosenRandomly();
this.Performed.add(k) ;
numberAttempts += 1;
}
return null;
}
List Performed=new ArrayList();
公共字符串dived(){
虽然(this.numberAttempts代替integer
数组,你必须使用ArrayList
否则它会做什么?崩溃?或者它只是给出了错误的输出?n
这里不能是=3
,所以我不明白替换n
会如何影响任何事情,除非这个e Negative我需要使用整数数组,因为这是我所知道的唯一有限制的数组方法。numberAttempts最初是0。只允许尝试3次。因此while循环运行,在每个循环之后,numberAttempts增加1。在每个循环中生成一个随机数。我希望记录每个随机数他被列入名单。