I'm new to Java, I have created an array of 15 random integers and I have found the minimum and maximum value. I now need to concatenate the index of the maximum and minimum value.
package homework7;
import java.util.Arrays;
import java.util.Random;
/**
*
* @author a
*/
public class Homework7 {
public static void main(String[] args) {
// Name and Homework Number
System.out.println("A" + "
Homework 7");
int[] numbers = new int[15];
//Generates 15 Random Numbers in the range 1 - 1000
for (int i = 0; i < numbers.length; i++) {
numbers[i] = (int) (Math.random() * 1000 - 1);
}//end for loop
System.out.println("
Contents of the Array:
" + Arrays.toString(numbers));
// Calling getMax() method for getting max value
int max = getMax(numbers);
System.out.println("
Maximum Value " + max + " is located at index ");
// Calling getMin() method for getting min value
int min = getMin(numbers);
System.out.println("Minimum Value " + min + " is located at index ");
}
// Method for getting the maximum value
public static int getMax(int[] inputArray) {
int maxValue = inputArray[0];
for (int i = 1; i < inputArray.length; i++) {
if (inputArray[i] > maxValue) {
maxValue = inputArray[i];
}
}
return maxValue;
}
// Method for getting the minimum value
public static int getMin(int[] inputArray) {
int minValue = inputArray[0];
for (int i = 1; i < inputArray.length; i++) {
if (inputArray[i] < minValue) {
minValue = inputArray[i];
}
}
return minValue;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…