Java ArrayList在循环结束后继续重置。我做错了什么?

Java ArrayList在循环结束后继续重置。我做错了什么?,java,arrays,loops,arraylist,while-loop,Java,Arrays,Loops,Arraylist,While Loop,每次我在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' o

每次我在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循环之前。

在循环的每个迭代中,您都在重新定义max。移动线路

ArrayList <Integer> max = new ArrayList<Integer>(); 
在外部和while循环之前

每次你说ArrayList max=new ArrayList

创建新的arraylist时,请确保只创建一次,然后继续添加

每次你说ArrayList max=new ArrayList


创建新的arraylist时,请确保只创建一次,然后继续添加新用户!我们都应该发表好的答案/评论。请不要投反对票,因为这对于一个刚开始的Java程序员来说是一个合理的问题。欢迎来到Stackoverflow!我希望你有一个愉快的时间问问题。新用户!我们都应该发表好的答案/评论。请不要投反对票,因为这对于一个刚开始的Java程序员来说是一个合理的问题。欢迎来到Stackoverflow!希望大家提问愉快,谢谢。真不敢相信我居然没想到。我是java新手,非常感谢您的帮助。谢谢。真不敢相信我居然没想到。我是java新手,非常感谢您的帮助。谢谢。真不敢相信我居然没想到。我是java新手,非常感谢您的帮助。谢谢。真不敢相信我居然没想到。我是java新手,非常感谢您的帮助。