/*
* One common programming activity is to find the minimum and maximum values
* within a list. In this challenge activity we will do just that. It will also
* demonstrate how arrays and for loops compliment each other nicely.
*
* First, execute the main() method as is so you can understand how the for loop
* works with the array. If you must, set a breakpoint and step through the code.
*
* Notice the min and max values are not correct. That's where you come in your
* job is to write these methods. Regardless of min or max, your approach should
* be the same: (Here's the pseudocode for min)
*
* set min to the value of the first element in the array
* for each element in the array
* if the current element is less than min
* set min to the current element
* end for
* return min
*/
package minandmax;
import java.util.Scanner;
public class MinAndMax {
public static void main(String[] args) {
Scanner input = new Scanner(System. in);
int[] array = new int[10];
// Read inputs into the array
System.out.println("Enter 10 Integers.");
for (int i = 0; i < array.length; i++) {
System.out.printf("Enter Integer %d ==>", i + 1);
array[i] = input.nextInt();
}
// Print out the array
System.out.print("You Entered :");
for (int i = 0; i < array.length; i++) {
System.out.printf("%d ", array[i]);
}
System.out.println();
// find the min/max and print
System.out.printf("Min = %d\n", getMin(array));
System.out.printf("Max = %d\n", getMax(array));
}
/**
* returns the smallest value in the array
* @param array array of integer
* @return integer representing the smallest
*/
public static int getMin(int[] array) {
//TODO: write code here
int min = array[0];
for (int a: array) {
if (a < min) {
min = a;
} else {
break;
}
}
return min;
}
/**
* returns the largest value in the array
* @param array array of integer
* @return integer representing the largest
*/
public static int getMax(int[] array) {
//TODO: write code here
int max = array[0];
for (int a: array) {
{
if (a > max) {
max = a;
return max;
}
我不斷收到缺少return語句,達到文件的末尾,而解析,但是我已經有我return語句和我的代碼正確關閉支架。請幫幫忙,謝謝「缺少return語句」,但我已經有return語句
你有*所有*代碼路徑的返回語句嗎? – hexafraction
發佈的代碼在方法中間切斷。 –
'「...並且我的代碼正確地關閉了括號。」 - 不是您發佈的代碼。嘗試發佈代碼的其餘部分,並指出錯誤發生在哪一行。 – azurefrog