Answer the question
In order to leave comments, you need to log in
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);
}
}
}
}
}
Answer the question
In order to leave comments, you need to log in
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
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);
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);
}
}
}
}
}
size()
i < array.length;
size()
to get the size of the list. 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);
}
}
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);
}
}
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);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question