作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
每次我循环遍历 arrayList 时,它都会自行重置。最终的目标是制作一个程序来查找 arrayList 中数字的平均值
public class loops {
public static void main(String[] args) {
System.out.println("Do you want to calculate the most reoccuring answer?");
System.out.println("enter 'yes' or 'no'");
Scanner start = new Scanner(System. in );
String starter = start.nextLine();
boolean jax = true;
while (jax == true && starter.equals("yes")) {
Scanner answer = new Scanner(System. in );
System.out.println("Enter the answer choice");
int ans = answer.nextInt();
ArrayList < Integer > max = new ArrayList < Integer > ();
max.add(ans);
Scanner goOn = new Scanner(System. in );
System.out.println("Any other grades?");
System.out.println("yes or no");
String procced = goOn.nextLine();
String str12 = "yes";
String str113 = "no";
if (procced.equals(str113)) {
jax = false;
System.out.print(max);
}
}
}
}
最佳答案
您要在循环的每次迭代中重新声明 max
。移动线
ArrayList <Integer> max = new ArrayList<Integer>();
在外部和 while
循环之前。
关于java - ArrayList 在循环后不断重置。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16247019/
我是一名优秀的程序员,十分优秀!