如何在 Java 中找到数组的最大数?
How to find largest number with arrays, in Java?
对于这项大学作业,我需要使用 public static int indexOfLargestElement(double[] array)
。
public class FindLargest {
public static void main(String[] args) {
public static int indexOfLargestElement(double[] array) {
return 0;
}
{
int numbers[] = new int[]{(int) 1.9,(int) 2.5,(int) 3.7,2,(int) 1.5, 6, 3, 4, 5, 2};
int largest1 = numbers[0];
for(int i =1;i<numbers.length;i++) {
int indexOfLargestElement;
if(numbers[i] > largest1)
largest1 = numbers[1];
}
}
System.out.println("Largest Element Number is: " + largest1);
return.largest;
}
尝试这样的事情
public static void main(String[] args) {
double numbers []= {1.9,2.5,3.7,2,1.5,6,3,4,5,2};
int index= indexOfLargestElement(numbers);
System.out.println("The index of the largest number is: "+index);
}
public static int indexOfLargestElement(double[] array){
int index=0;
double largest=0;
for (int i = 0; i < array.length; i++) {
if (array[i]>largest){
largest= array[i];
index=i;
}
}
return index;
}
对于这项大学作业,我需要使用 public static int indexOfLargestElement(double[] array)
。
public class FindLargest {
public static void main(String[] args) {
public static int indexOfLargestElement(double[] array) {
return 0;
}
{
int numbers[] = new int[]{(int) 1.9,(int) 2.5,(int) 3.7,2,(int) 1.5, 6, 3, 4, 5, 2};
int largest1 = numbers[0];
for(int i =1;i<numbers.length;i++) {
int indexOfLargestElement;
if(numbers[i] > largest1)
largest1 = numbers[1];
}
}
System.out.println("Largest Element Number is: " + largest1);
return.largest;
}
尝试这样的事情
public static void main(String[] args) {
double numbers []= {1.9,2.5,3.7,2,1.5,6,3,4,5,2};
int index= indexOfLargestElement(numbers);
System.out.println("The index of the largest number is: "+index);
}
public static int indexOfLargestElement(double[] array){
int index=0;
double largest=0;
for (int i = 0; i < array.length; i++) {
if (array[i]>largest){
largest= array[i];
index=i;
}
}
return index;
}