J
J
Janna16032020-05-29 14:41:00
Java
Janna1603, 2020-05-29 14:41:00

How to end the "Maximum Value" program?

import java.util.ArrayList;
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Integer> array = new ArrayList<>();
        
        System.out.println("Введите числа");
        
        while (true) {
            int number = input.nextInt();
            array.add(number);
            
            if (number == 0) {
                break;
            }
            
            int max = 0;
            
            for (int i = 0; i < array.length; i++) {
                if (array.get(i) > max) {
                    max = array.get(i);
                }
            }
        }
    }
}


I've been trying to solve this problem for three days now. We have to enter numbers, when we enter zero, the program stops and compares the numbers, and then displays the maximum value. I searched on the Internet how to find the maximum value in an array, for everyone through .length, but it doesn’t work for me, it is highlighted in red.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
Billy Milligan, 2020-05-29
@Billy_Milligan

The ArrayList class does not have a field length(If it were an int[] array, you can use length), to determine the size of a List, you need to use the size().
for([initialization] ; [condition under which we continue the loop] ; [step])
How it works

  1. [initialization] - executed the very first and only 1 time.
  2. [condition under which we continue the loop] - check the condition
  3. (execute the code inside the loop)
  4. [step]
  5. [condition under which we continue the loop] - check the condition
  6. (execute the code inside the loop)
  7. [step]
  8. ...

Scanner input = new Scanner(System.in);
ArrayList<Integer> array = new ArrayList<>();
int max = 0;

System.out.println("Введите числа");

for(int number = input.nextInt() ; number != 0; number = input.nextInt()) {
     array.add(number);
}

for (int i = 0; i < array.size(); i++) {
      if (array.get(i) > max) {
           max = array.get(i);
      }
}

System.out.println("Это число: "+max);

O
Orkhan, 2020-05-29
Hasanly @azerphoenix

Hello!
Here is your source code:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList array = new ArrayList<>();
        System.out.println("Введите числа");
        while (true) {
            int number = input.nextInt();
            array.add(number);
            if (number == 0) {
                break;
            }
            int max = 0;
            
            for (int i = 0; i < array.length; i++) {
                if (array.get(i) > max) {
                    max = array.get(i);
                }
            }

        }
    }

}

You are trying to get the size using length, but you are using an ArrayList object. For ArrayList, you need to use to get the size size()
Error:
i < array.length;
Either create an array of numbers, or if you use ArrayList (list array), then call the method size()to get the size of the list.
Also note that you are trying to compare numbers while inside the loop, but before the comparison code is executed, you enter zero and exit their array.
Added comments to the code:
if (number == 0) {
                break; // вышли из цикла
            }

            // до сюда код не исполнится, так как выше break и вы вышли из цикла
            int max = 0;
            for (int i = 0; i < array.length; i++) {
                if (array.get(i) > max) {
                    max = array.get(i);
                }
            }

And here you need to pass a parameterized type (preferably)
Before - After - As for getting the maximum value of a number, why not use the capabilities of the Math package? Specifically the method or for example the max() method in Collections. Here is the working code:
ArrayList array = new ArrayList<>();
ArrayList<Integer> array = new ArrayList<>();
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        ArrayList<Integer> array = new ArrayList<>();
        System.out.println("Введите числа");

        while (true) {
            int number = input.nextInt();
            array.add(number);

            if (number == 0) {
                break;
            }
        }

        int maxValue = Collections.max(array);
        System.out.println("Максимальное число " + maxValue);

    }

}

If you want to find max. value itself by iterating over the list, then here is another solution
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        ArrayList<Integer> array = new ArrayList<>();
        System.out.println("Введите числа");

        while (true) {
            int number = input.nextInt();
            array.add(number);

            if (number == 0) {
                break;
            }
        }

        /*int maxValue = Collections.max(array);
        System.out.println("Максимальное число " + maxValue);*/

        int maxValue = 0;
        for (int i : array) {
            if (array.get(i) > maxValue) {
                maxValue = array.get(i);
            }
        }
        System.out.println("Максимальное число " + maxValue);
    }

}

G
GavriKos, 2020-05-29
@GavriKos

https://docs.oracle.com/javase/8/docs/api/java/uti...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question