“缺少退货声明”,但我已经有了退货声明

时间:2015-04-07 18:55:46

标签: java arrays

/*
 * 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语句并且我的代码正确地关闭了括号。请帮助,谢谢

4 个答案:

答案 0 :(得分:3)

getMax方法中,如果return max

,则会收到错误,因为无法访问(a <=max)

答案 1 :(得分:0)

查看getMax,你有两个开放的大括号

public static int getMax(int[] array) {
    //TODO: write code here
    int max = array[0];
    for(int a : array) 
    {
        {    <===== HERE
            if (a > max ) 
            {

在旁注中,当您遇到大于max的数字时,您不应该立即返回max,您应该等到所有人都看到它们。

答案 2 :(得分:0)

如果if条件完全不正确,或者如果控件永远不会进入forloop,那么方法getMax()将不会返回任何内容。但是根据方法定义,它应该返回一个整数。并且很少有支架没有正确关闭/打开。

试试这个

public static int getMax(int[] array) {
    int max = array[0];
    for (int a: array) {
        if (a > max) {
            max = a;
        }
    }
    return max;
}

答案 3 :(得分:0)

只要想想你的代码在你的2种查找方法中所做的事情。 你永远不会以你实现它的方式测试数组中的所有int值。

只需使用简单的if(min