Java 我的代码包含错误和运行时忘记声明的错误?
我不知道如何修复这个错误,甚至不知道这意味着什么。我的结果需要如下所示:Java 我的代码包含错误和运行时忘记声明的错误?,java,Java,我不知道如何修复这个错误,甚至不知道这意味着什么。我的结果需要如下所示: 猜测最大整数值: 543214 你在2146940433之前离开了 猜测最小整数值: 2145678321 你在1805327前离开了 我已经评论了你的错误,但没有测试它 public static void main(String[] args) { // Create a Scanner object Scanner input = new Scanner(System.in); // Cr
猜测最大整数值:
543214
你在2146940433之前离开了
猜测最小整数值:
2145678321
你在1805327前离开了
我已经评论了你的错误,但没有测试它
public static void main(String[] args)
{
// Create a Scanner object
Scanner input = new Scanner(System.in);
// Create an Extremes object
Extremes extreme = new Extremes();
// Ask the user to guess the maximum value of an Integer
System.out.println("Guess the maximum Integer value: ");
int max = input.nextInt(); // your first error, assign input to integer,
// Compute and display the difference
// between the max and the guess
System.out.print("You were off by " + extreme.maxDiff(max)); // your second error, invoke emthod by ref
// Ask the user to guess the minimum value of an Integer
System.out.println("Guess the minimum Integer value: ");
int min = input.nextInt(); // yout third error assign input to int
// Compute and display the difference
// between the min and the guess
System.out.println("You were off by " + extreme.minDiff(min)); // and again invoke method correctly
}
您必须将输入分配给int,然后从类中调用方法来查找dif,我已经对您的错误进行了注释,但没有对其进行测试
public static void main(String[] args)
{
// Create a Scanner object
Scanner input = new Scanner(System.in);
// Create an Extremes object
Extremes extreme = new Extremes();
// Ask the user to guess the maximum value of an Integer
System.out.println("Guess the maximum Integer value: ");
int max = input.nextInt(); // your first error, assign input to integer,
// Compute and display the difference
// between the max and the guess
System.out.print("You were off by " + extreme.maxDiff(max)); // your second error, invoke emthod by ref
// Ask the user to guess the minimum value of an Integer
System.out.println("Guess the minimum Integer value: ");
int min = input.nextInt(); // yout third error assign input to int
// Compute and display the difference
// between the min and the guess
System.out.println("You were off by " + extreme.minDiff(min)); // and again invoke method correctly
}
您必须将输入分配给int,然后从类中调用方法来查找dif,至少它是:extreme.maxDiff()和extreme.minDiff()。这些是来自Extreme类的方法,因此必须对Extreme对象调用它们。@WesleyDekeirsmaker当我放置Extreme.maxDiff()和Extreme.minDiff()时,它给出了一个错误“方法应为整型,而类型未提供参数”。对于数值,请使用
int
,而不是Integerint
是一个原语(只有四个字节),而Integer是一个完整的对象。使用整数对象进行数学运算可能涉及大量对象创建和垃圾收集,这可能会影响性能。即使Integer.MIN_值和Integer.MAX_值都是int
值,而不是Integer对象,尽管它们是在Integer类中声明的。至少它是:extreme.maxDiff()和extreme.minDiff()。这些是来自Extreme类的方法,因此必须对Extreme对象调用它们。@WesleyDekeirsmaker当我放置Extreme.maxDiff()和Extreme.minDiff()时,它给出了一个错误“方法应为整型,而类型未提供参数”。对于数值,请使用int
,而不是Integerint
是一个原语(只有四个字节),而Integer是一个完整的对象。使用整数对象进行数学运算可能涉及大量对象创建和垃圾收集,这可能会影响性能。即使Integer.MIN_值和Integer.MAX_值都是int
值,而不是Integer对象,尽管它们在Integer类中声明。