我是Java的新手,我創建了一個15個隨機整數的數組,並且我找到了最小值和最大值。我現在需要連接最大值和最小值的索引。查找隨機索引
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" + " \nHomework 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("\nContents of the Array:\n" + Arrays.toString(numbers));
// Calling getMax() method for getting max value
int max = getMax(numbers);
System.out.println("\nMaximum 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;
}
}
那麼究竟是什麼問題? – Mureinik
歡迎來到Stack Overflow!看起來你正在尋求作業幫助。雖然我們與本身沒有任何問題,請遵守這些[該做什麼和不該做什麼(http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845),並相應地編輯您的問題。 –
找到的最小和這個隨機序列的最大值的指數 –