函数方法

时间:2016-11-22 04:13:04

标签: java loops methods java.util.scanner

注意:希望将其移至Code Review,并使用更清晰的答案结构和修改后的代码,这与除了calcmin方法之外的答案非常相似。

我正在尝试将这些代码分解为多种方法,并且我在第一位获得了成功,但其他两种方法似乎无法弄明白。

使用第二种方法,我试图做到这一点,它会询问用户一个整数,并不断提示它们,直到输入一个正确的整数。

使用第三种方法,我试图使它需要三个整数参数并返回这些参数的最小值。

我真的很感激这方面的帮助。我查看了我的书中的例子,似乎无法得到它。

我的代码:

import java.util.Scanner;

public class MinOfThreeInts 
{
    public static void intro ()
    {
        System.out.println("This program determines the minimum of three ints");
        System.out.println("It gracefully reports errors when erroneous data is entered ");
        System.out.println("For example, if you type in 'abc' when this program asked for an int");
        System.out.println("the program will report the error & ask for another int");
        System.out.println("Try giving it bad input ! \n\n");

    }
    public static void readInt (int value1, int value2, int value3)
    {
        System.out.print(" Please enter an integer value ");
        Scanner console = new Scanner(System.in);
        String input = console.nextLine();
        Boolean goodInt;
        int parsedValue = 0;
        goodInt = false;
        while (!goodInt)
        {
            try
            {
                parsedValue = Integer.parseInt(input);
                goodInt = true;
            }
            catch(NumberFormatException ex)
            {
                System.out.print("    Invalid input, please enter Int ");
                input = console.nextLine();
            }
        }
        value1 = parsedValue;

        // Get the second integer

        System.out.print(" Please enter an integer value ");
        input = console.nextLine();
        goodInt = false;
        while (!goodInt)
        {
            try
            {
                parsedValue = Integer.parseInt(input);
                goodInt = true;
            }
            catch(NumberFormatException ex)
            {
                System.out.print("      Invalid input, please enter Int ");
                input = console.nextLine();
            }
        }
        value2 = parsedValue;

        // Get the third integer

        System.out.print(" Please enter an integer value ");
        input = console.nextLine();
        goodInt = false;
        while (!goodInt)
        {
                try
            {
                parsedValue = Integer.parseInt(input);
                goodInt = true;
            }
            catch(NumberFormatException ex)
            {
                System.out.print("     Invalid input, please enter Int ");
                input = console.nextLine();
            }
        }
        value3 = parsedValue;
    }

    public static void calcMin (min)
    {
        int min = value1;
        if (value2 < min)
        {
            min = value2;
        }
        if (value3 < min)
        {
            min = value3;
        }

    // Now report the results
    System.out.println(" The minimum value of the three ints is " + min);
    }

    public static void main(String[] args) 
    {
        value1 = readInt(console);
        value2 = readInt(console);
        value3 = readInt(console);

        min = calcMin(value1,value2,value3); 
    }
}

1 个答案:

答案 0 :(得分:2)

你有几个问题。我重构了您的代码并添加了评论,我保持接近您的代码,以便为您提供有关可以改进的地方的见解。

首先,代码:

   import java.util.Arrays;
   import java.util.Collections;
   import java.util.List;
   import java.util.Scanner;

   public class MinOfThreeInts {

        //the main method, things start here
        public static void main(String[] args) {

            //initialize a new scanner that the application will use
            Scanner console = new Scanner(System.in);

            //print the intro
            intro();

            //read the values one by one and save them in a variable
            int value1 = readInt(console);
            int value2 = readInt(console);
            int value3 = readInt(console);

            //calculate the minimum and save it in the min variable
            int min = calcMin(Arrays.asList(value1,value2,value3));

            // Now report the results
            System.out.println(" The minimum value of the three ints is " + min);

        }

        /**
         * Reads an integer from the given console
         */
        public static int readInt(Scanner console) {

            System.out.print(" Please enter an integer value ");

            //read the input
            int parsedValue = 0;
            boolean goodInt = false;

            //as long as we don't find a valid number
            while (!goodInt)
            {
                try
                {
                    //read the input
                    String input = console.nextLine();

                    //try to parse the value
                    parsedValue = Integer.parseInt(input);

                    //set goodInt to true so that the while loop will end
                    goodInt = true;
                }
                catch(NumberFormatException ex)
                {
                    //if the provivded value was not an integer, print a message and return to the start of the while loop
                    System.out.print("      Invalid input, please enter Int ");
                }
            }

            return parsedValue;

        }

        /**
         * calculates the minimum of a list of values
         */
        public static int calcMin (List<Integer> values) {

            //find the minimum and return the value
            return Collections.min(values);

        }

        /**
         * prints an intro message
         */
        public static void intro () {
            System.out.println("This program determines the minimum of three ints");
            System.out.println("It gracefully reports errors when erroneous data is entered ");
            System.out.println("For example, if you type in 'abc' when this program asked for an int");
            System.out.println("the program will report the error & ask for another int");
            System.out.println("Try giving it bad input ! \n\n");

        }

    }

现在,您可以采取哪些措施来改进:

  • 编译代码无法编译,请始终注意您的代码编译,然后稍微编辑它,直到再次编译

  • 范围你的代码有多个声明的整数,问题是这些值在其他方法中是不可见的,如果你声明一个变量,在某些方法中说int value1,另一个方法将无法看到它。如果在另一个方法中有另一个int value1,它只会在该特定方法中可见,它实际上是另一个变量

  • 参数与返回类型方法获取参数,返回某些内容。参数是方法的输入,返回的值是方法的结果。以您的方法为例:public static void readInt (int value1, int value2, int value3)。这是一个应该从控制台读取整数值的方法。但是,此方法签名表示它需要3个整数作为参数。这些整数将按值传递,因为它们是基本类型,因此您无法传递它们,然后填充它们然后返回它们。也没有返回类型,因此该方法不返回某些内容。由于整数参数value1,value2和value3仅在方法范围内可见,因此您将丢失数据。与新签名进行比较:public static int readInt(Scanner console)。此方法使控制台从作为参数读取并返回一个整数,即已读取的数字。此方法封装了重试。