Task : An array consists of natural numbers.

The number of elements is unknown, but if the i-th element of the array is == 0, then the reading of elements from the keyboard is stopped and the maximum element of the array is output.

Preface : Did not work before with the dynamic array ListArray

My code is :

int rez = 1; int max=0; ArrayList<Integer> b = new ArrayList<>(); b.ensureCapacity(100); System.out.println("Введите элементы"); while(!(rez==0)){ int j; for (j = 0; j<b.size(); j++) { b.set(j, in.nextInt()); if(!(b.get(j)==0)){ if(b.get(j)>max){b.set(j, b.get(j));} }else{ rez=0; break; } } } System.out.println("Максимальный элемент вашего массива = " + max); 
  • I was told about the fact that the exit from the cycle should be different, but how not transformed it is useless. - Marat Zimnurov

1 answer 1

Here is your solution:

 System.out.println("Введите элементы"); Scanner scanner = new Scanner(System.in); int number; ArrayList<Integer> numbers = new ArrayList<>(); while ((number = scanner.nextInt())!=0) numbers.add(number); System.out.println("Максимальный элемент вашего массива = " + Collections.max(numbers));