“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)));
}
}
最后我使用它在同一个程序中但在另一个文件夹中运行它。