-3

“public static int readInt(String s)”这在程序中到底是做什么的?目前我需要使用其中的 4 个,但它们都不同,对于我在这里出现的那个,我需要制作一个程序,在该程序中提示用户某些内容,如果输入是 int,那么它会被显示,然后转到第二个为用户输入,如果正确则继续,否则如果不是 int;如果它是双精度的,则打印出“这无效...”,然后将再次重复第一个输入,并且如果用户提示的第二个输入不是整数,也会对第二个输入执行相同的操作。用户将总共有两个输入。我应该怎么做才能为此制作程序?我对如何使用这个“public static int readInt(String s)”感到困惑。

包有理数;

导入 java.util.*;

公共类实用程序 { public static int readInt(String s) {

} 

public static double readDouble(String s)

/**
 * Generates a random integer between min and max, inclusive
 * Precondition: min <= max
 * @param min lower bound for the random integer
 * @param max upper bound for the random integer
 * @return A random integer
 */
public static int randomInt(int min, int max)
{

}

/**
 * Computes the gcd between 2 nonnegative integers
 * Precondition: num1 >= 0, num2 >= 0
 * @param num1 The first integer
 * @param num2 The second integer
 * @return the gcd of num1 and num 2, gcd is 1 if both are 0, 
 *          gcd is the non-zero number if only one is 0.
 */
public static int gcd(int num1, int num2)
{

}

这就像代码的一部分....到目前为止,我还需要在此处添加注释,这些是我的代码的最后两部分,但是如果没有前两部分,我将无法启动它。

包有理数;

/** * Utility 类的测试程序 */ public class UtilityTest {

public static void main(String[] args)
{
    String prompt1 = "Enter first integer: ";
    String prompt2 = "Enter second integer: ";

    int a = Utility.readInt(prompt1);
    int b = Utility.readInt(prompt2);

    int small = Math.min(a, b);
    int large = Math.max(a, b);

    System.out.println("A few random integers: ");
    System.out.println(Utility.randomInt(small, large));
    System.out.println(Utility.randomInt(small, large));
    System.out.println(Utility.randomInt(small, large));
    System.out.println(Utility.randomInt(small, large));

    System.out.printf("The gcd of %d and %d is ", a, b);
    System.out.println(Utility.gcd(Math.abs(a), Math.abs(b)));    
}    

}

最后我使用它在同一个程序中但在另一个文件夹中运行它。

4

2 回答 2

1

分解它:

Public-- 表示这是一个公共方法

int-- 表示该函数将return是一个整数

readInt-- 函数名

String s-- 表示函数采用String将在函数内引用的类型参数s

于 2013-10-15T00:15:47.890 回答
0

Public--公共方法

int-- 返回 tpye 整数

readInt--读取字符串并将其作为整数返回的函数的名称。在您的程序中,它将读取 prompt1 和 prompt2,就像从用户那里获取输入一样,并将它们存储在 int a 和 int b 中。

String s -- 表示该函数采用 String 类型的参数,该参数将在函数中称为 s。

于 2013-10-15T01:21:21.133 回答