I'm trying to randomly generate 5 numbers and print the largest one, it's okay to generate numbers but I get an error when trying to print out the largest number
My Code:
import java.util.Arrays;
import java.util.Random;
public class Project {
public static void main(String[] args) {
list(0, 0);
printMax();}
private static void list(int min, int max) {
int[] numbers = new int[5];
for (int i = 0; i<numbers.length; i++) {
numbers[i] = (int)(Math.random() * 100 + 1);}
System.out.println("Sayilar: " + Arrays.toString(numbers));
return;}
private static void printMax(int... numbers) {
int result = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > result) {
result = numbers[i];}}
System.out.println("\nThe largest value is " + result);
return;}
}
the output I got:
Sayilar: [5, 48, 88, 28, 68]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Project.printMax(Project.java:20)
at Project.main(Project.java:8)
Aucun commentaire:
Enregistrer un commentaire