2013-04-27 3 views
1

arrayList를 반복 할 때마다 그 자체가 재설정됩니다. 결국 목표는 당신은 루프의 각 반복에서 max를 재 선언하고 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); 
      } 
     } 
    } 
} 
+1

신규 사용자! 우리는 모두 좋은 답변/의견을 게시해야합니다. 이것이 처음 자바 프로그래머에게 합당한 질문이기 때문에 다운 voting을 자제하십시오. Stackoverflow에 오신 것을 환영합니다! 즐거운 시간을 갖기를 바랍니다. – Justin

답변

3

의 숫자의 평균을 발견하는 프로그램을 만드는 것입니다.

ArrayList <Integer> max = new ArrayList<Integer>(); 

외부 이전과 while 루프의 전으로 라인을 이동합니다.

+0

감사합니다. 나는 내가 그 생각을하지 않았다는 것을 믿을 수 없다. 나는 자바에 상당히 익숙하며 도움을 주셔서 감사합니다. – userJoe

2
ArrayList <Integer> max = new ArrayList<Integer>(); 
max.add(ans); 

언제나 ArrayList max = new ArrayList();

새로운 arraylist를 만들려면 한 번만 추가하고 계속 추가하십시오.

+0

고맙습니다. 나는 내가 그 생각을하지 않았다는 것을 믿을 수 없다. 나는 자바에 상당히 익숙하며 도움을 주셔서 감사합니다. – userJoe

관련 문제