gpt4 book ai didi

java - 函数方法

转载 作者:行者123 更新时间:2023-12-02 03:13:20 25 4
gpt4 key购买 nike

注意:希望将其移至代码审查,并提供更清晰的答案结构和我修改后的代码,除了 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.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)。此方法采用要读取的控制台作为参数,并返回一个整数,即已读取的数字。该方法封装了重试。

关于java - 函数方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40733646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com