[英]Trying to output an if else if, else loop. using arrays and Scanners. What am i doing wrong?
[英]ArrayList keeps resetting after loop. what am I doing wrong?
每當我遍歷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
循環之前和之外。
ArrayList <Integer> max = new ArrayList<Integer>();
max.add(ans);
每次您說ArrayList max = new ArrayList();
您創建新的arraylist,請確保執行一次並繼續添加
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.