简体   繁体   中英

(Java ) Finding the largest number in an array and it's location

I'm currently learning Java and while I'm able to find the largest number I'm stuck on how to find it's location. Any help would be greatly appreciated!

import java.util.Random;
public class FindingLargestValueInAnArray {
    public static void main(String[] args) {
        Random number = new Random();

        int[] array_1 = new int[10];

        int i = 0;
        for (i = 0; i < array_1.length; i++) {
            int randNum = 1 + number.nextInt(99);
            array_1[i] = randNum;
        }

        System.out.print("Array:");

        for (i = 0; i < array_1.length; i++) {
            System.out.print(" " + array_1[i]);
        }

        int largeNumb = 0;

        for (i = 0; i < array_1.length; i++) {
            if (array_1[i] > largeNumb) {
                largeNumb = array_1[i];
            }
        }

        System.out.println("\n\nThe largest value is 
            "+largeNumb);
        }
    }
    public class FindingLargestValueInAnArray {
        public static void main(String[] args) {
        Random number = new Random();

        int[] array_1 = new int[10];

        int i = 0;
        for (i = 0; i < array_1.length; i++) {
            int randNum = 1 + number.nextInt(99);
            array_1[i] = randNum;
        }

        System.out.print("Array:");

        for (i = 0; i < array_1.length; i++) {
            System.out.print(" " + array_1[i]);
        }

        int largeNumb = 0;
        int index = 0;

        for (i = 0; i < array_1.length; i++) {
            if (array_1[i] > largeNumb) {
                largeNumb = array_1[i];
                index = i;
            }
        }

        System.out.println("The largest value is " + largeNumb + " and it's location is" + index);
    }
}

you can make an int index=0 in the loop where you check if the number is bigger in the body of the if make infex=i. This way you will always have both the value and the index of the largest numnber.

You should get an another variable for the index = 0 and then put it inside the if statement and make it equal to index = i; . After the loop ends the largest value index would be stored here.

import java.util.Random;

public class FindingLargestValueInAnArray {
    public static void main(String[] args) {
        Random number = new Random();

        int[] array_1 = new int[10];

        int i = 0;
        for (i = 0; i < array_1.length; i++) {
            int randNum = 1 + number.nextInt(99);
            array_1[i] = randNum;
        }

        System.out.print("Array:");

        for (i = 0; i < array_1.length; i++) {
            System.out.print(" " + array_1[i]);
        }

        int largeNumb = array_1[0];
        int index = 0;
        for (i = 1; i < array_1.length; i++) {
            if (array_1[i] > largeNumb) {
                largeNumb = array_1[i];
                index = i;
            }
        }

        System.out.println("\n\nThe largest value is :" + largeNumb + " and it's index is: " + index);
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM