Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 函数的方法_Java_Loops_Methods_Java.util.scanner - Fatal编程技术网

Java 函数的方法

Java 函数的方法,java,loops,methods,java.util.scanner,Java,Loops,Methods,Java.util.scanner,注意:希望将此移到代码审查,以更清晰的答案结构和我修改的代码,除了calcmin方法外,与答案非常相似。 我试图将这段代码分解成多个方法,第一个部分我很成功,但其他两个部分我似乎无法理解 对于第二个方法,我试图使其成为这样,它会要求用户输入一个整数,并不断提示他们,直到输入一个正确的整数 对于第三个方法,我试图使它接受三个整数参数并返回这些参数的最小值 我非常感谢你在这方面的帮助。我已经在我的书中看过了一些例子,但似乎不明白 我的代码: import java.util.Scanner; pu

注意:希望将此移到代码审查,以更清晰的答案结构和我修改的代码,除了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); 
    }
}
import java.util.Scanner;
公共课三年级学生
{
公共静态void简介()
{
System.out.println(“此程序确定至少三个整数”);
System.out.println(“当输入错误数据时,它优雅地报告错误”);
System.out.println(“例如,当这个程序要求输入int时,如果您输入'abc'”);
System.out.println(“程序将报告错误并请求另一个int”);
System.out.println(“尝试给它错误的输入!\n\n”);
}
公共静态void readInt(int值1、int值2、int值3)
{
系统输出打印(“请输入整数值”);
扫描仪控制台=新扫描仪(System.in);
字符串输入=console.nextLine();
布尔goodInt;
int parsedValue=0;
goodInt=false;
而(!goodInt)
{
尝试
{
parsedValue=Integer.parseInt(输入);
goodInt=true;
}
捕获(NumberFormatException ex)
{
系统输出打印(“输入无效,请输入Int”);
input=console.nextLine();
}
}
value1=解析的值;
//获取第二个整数
系统输出打印(“请输入整数值”);
input=console.nextLine();
goodInt=false;
而(!goodInt)
{
尝试
{
parsedValue=Integer.parseInt(输入);
goodInt=true;
}
捕获(NumberFormatException ex)
{
系统输出打印(“输入无效,请输入Int”);
input=console.nextLine();
}
}
value2=解析的值;
//获取第三个整数
系统输出打印(“请输入整数值”);
input=console.nextLine();
goodInt=false;
而(!goodInt)
{
尝试
{
parsedValue=Integer.parseInt(输入);
goodInt=true;
}
捕获(NumberFormatException ex)
{
系统输出打印(“输入无效,请输入Int”);
input=console.nextLine();
}
}
value3=解析的值;
}
公共静态无效calcMin(最小值)
{
int min=值1;
如果(值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");

        }

    }
导入java.util.array;
导入java.util.Collections;
导入java.util.List;
导入java.util.Scanner;
公共课三年级学生{
//主要的方法,事情从这里开始
公共静态void main(字符串[]args){
//初始化应用程序将使用的新扫描仪
扫描仪控制台=新扫描仪(System.in);
//打印介绍
简介();
//逐个读取值并将其保存在变量中
int value1=readInt(控制台);
int value2=readInt(控制台);
int value3=readInt(控制台);
//计算最小值并将其保存在min变量中
int min=calcMin(Arrays.asList(value1、value2、value3));
//现在报告结果
System.out.println(“三个整数的最小值为”+min);
}
/**
*从给定控制台读取整数
*/
公共静态int readInt(扫描仪控制台){
系统输出打印(“请输入整数值”);
//读取输入
int parsedValue=0;
布尔值goodInt=false;
//只要我们找不到有效的号码
而(!goodInt)
{
尝试
{
//读取输入
字符串输入=console.nextLine();
//尝试解析该值
parsedValue=Integer.parseInt(输入);
//将goodInt设置为true,以便while循环结束
goodInt=true;
}
捕获(NumberFormatException ex)
{
//如果提供的值不是整数,则打印一条消息并返回while循环的开头
系统输出打印(“输入无效,请输入Int”);
}
}
返回parsedValue;
}
/**
*计算值列表的最小值
*/
公共静态int calcMin(列表值){
//求最小值