I am trying to make methods that takes numbers into an array, but I'm stuck on how to return the index of biggest/smallest value "as array".
public static void main(String[] args) {
array();
int max[];
}
public static void array() {
Scanner input = new Scanner(System.in);
System.out.println("Length of the Array");
int x = input.nextInt();
int array[] = new int[x];
System.out.println("chose the numbers");
for (int i = 0; i < x; i++) {
array[i] = input.nextInt();
}
}
public static int max(int[] array) {
int max = 0;
int index = -1;
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
index = i;
}
{
return index;
The first part is working for the length and and numbers in the array, but the return index of largest small value as array. I am not sure how to proceed.
This will prompt you for to enter your array and then System.out.println the min and max.
I just modified the code from this link:
https://www.sanfoundry.com/java-program-find-largest-number-array/
import java.util.Scanner;
public class Largest_Smallest {
public static void main(String[] args)
{
int n, max, min;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter elements of array:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
max = a[0];
for(int i = 0; i < n; i++){
if(max < a[i])
{
max = a[i];
}
}
min = max;
for(int i = 0; i < n; i++){
if(min > a[i])
{
min = a[i];
}
}
System.out.println("Maximum value:"+max);
System.out.println("Maximum value:"+min);
}
}
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.