我开始学习考试并用方法做一些练习课程,我的脑海里现在空白了。我想知道如何初始化n1,n2,n3和n4。我将它们设置为0但返回语句仅返回0。
public class LargestOfIntegers2
{
public static int findLargest(int n1, int n2, int n3, int n4)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the first integer --> ");
n1 = scan.nextInt();
System.out.print("Enter the second integer --> ");
n2 = scan.nextInt();
System.out.print("Enter the third integer --> ");
n3 = scan.nextInt();
System.out.print("Enter the fourth integer --> ");
n4 = scan.nextInt();
if(n1>n2 && n1 > n3 && n1 > n4)
return n1;
else if(n2 > n1 && n2 > n3 && n2 > n4)
return n2;
else if(n3>n1 && n3>n2 && n3>n4)
return n3;
else
return n4;
}
public static void main(String[] args) {
int n1, n2, n3, n4;
findLargest(n1, n2, n3, n4);
if(n1>n2 && n1 > n3 && n1 > n4)
System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n1);
else if(n2 > n1 && n2 > n3 && n2 > n4)
System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n2);
else if(n3>n1 && n3>n2 && n3>n4)
System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n3);
else
System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n4);
}
}
答案 0 :(得分:0)
您的代码无效的主要原因:
public static void main(String[] args) {
int n1, n2, n3, n4;
findLargest(n1, n2, n3, n4); <--- YOU DID NOT STORE THE RETURNED VALUE
if(n1>n2 && n1 > n3 && n1 > n4)
System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n1);
else if(n2 > n1 && n2 > n3 && n2 > n4)
System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n2);
else if(n3>n1 && n3>n2 && n3>n4)
System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n3);
else
System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n4);
}
要么:
//prompt user input for n1, n2, n3, n4 first
System.out.println(findLargest(n1,n2,n3,4));
或强>
//prompt user input for n1, n2, n3, n4 first
int largest = findLargest(n1,n2,n3,n4);
System.ouot.println(largest);
我相信这会让你的代码有效。当然,在调用方法之前,不应在方法中提示用户输入,而应在方法之外提示它。
Java方法如何传递其值的说明:
public static void main(String[] args)
{
int n = 100;
someMethod(n);
System.out.println(n); //n will still be 100;
}
public static void someMethod(int n1)
{
n1 = 999;
}
为什么主{0}中的n
仍然是100,而不是999.这是因为Java按值传递所有内容。对于基本类型,传递的值是实际值本身。意味着将实际值的副本传递给方法。制作了一份副本。
因此n1
(范围: someMethod )只是n
的副本(范围:主要)。
n1
上的更改不会影响 n
。
为您提供额外的帮助:
如果您传递了4个变量n1, n2, n3, n4
。为什么要求用户在方法中再次输入4个值?
这样做将“无效”从方法的调用者传入的n1,n2,n3,n4的值。
顺便说一下,有很多简单的方法可以实现这一点。通过不触及数组,您可以简单地执行此操作:
public static int findLargest(int n1, int n2, int n3, int n4)
{
return Math.max(Math.max(n1,n2), Math.max(n3,n4));
}
答案 1 :(得分:0)
这是您的代码将执行的操作: 这将得到新的n1和n2以及n3和n4,并将它们设置为等于你传递的值
public static int findLargest(int n1, int n2, int n3, int n4){
{
您传递了未初始化的值,因此它们未初始化 然后用用户输入初始化它们(当你的变量是本地的时候,这个方法以外的任何一个变化)你找到最大的并返回它 你的代码在任何地方都存储了这个价值
int thelargest=findLargest(n1, n2, n3, n4);
会存储它
然后尝试比较UNINITIALIZED INTS,因为findlargest的方法无法看到n1 n2 n3 n4,因为没有通过引用来修复代码以执行我认为你想做的事情
static int n1,n2,n3,n4;
public static void findLargest()
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the first integer --> ");
n1 = scan.nextInt();
System.out.print("Enter the second integer --> ");
n2 = scan.nextInt();
System.out.print("Enter the third integer --> ");
n3 = scan.nextInt();
System.out.print("Enter the fourth integer --> ");
n4 = scan.nextInt();
//nothing is listening for these numbers anyway why bother with it
// if(n1>n2 && n1 > n3 && n1 > n4)
// return n1;
// else if(n2 > n1 && n2 > n3 && n2 > n4)
// return n2;
// else if(n3>n1 && n3>n2 && n3>n4)
// return n3;
// else
// return n4;
}
public static void main(String[] args) {
//int n1, n2, n3, n4;
findLargest();
if(n1>n2 && n1 > n3 && n1 > n4)
System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n1);
else if(n2 > n1 && n2 > n3 && n2 > n4)
System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n2);
else if(n3>n1 && n3>n2 && n3>n4)
System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n3);
else
System.out.println("Out of the numbers " + n1 + ", " + n2 + ", " + n3 + ", " + n4 + ", the largest integer is " + n4);
}
}
答案 2 :(得分:0)
变量n1
到n4
将在findLargest
函数中设置,但只在其本地副本中设置,更改将永远不会“回显”回{{1}功能。这是你的主要问题,因为main
中的变量因此没有被设置。
最好在函数中询问每个变量并返回它,然后通过获取返回值正确使用main
。那会是这样的:
findLargest
您可以看到我也对import java.util.Scanner;
public class Test {
public static int getNum(Scanner sc, String desc) {
System.out.print("Enter the " + desc + " integer --> ");
return sc.nextInt();
}
public static int findLargest(int n1, int n2, int n3, int n4) {
if (n1 >= n2 && n1 >= n3 && n1 >= n4)
return n1;
if (n2 >= n3 && n2 >= n4)
return n2;
if (n3 >= n4)
return n3;
return n4;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = getNum(scan, "first");
int b = getNum(scan, "second");
int c = getNum(scan, "third");
int d = getNum(scan, "fourth");
int x = findLargest(a, b, c, d);
System.out.println("max(" + a + "," + b + "," + c + "," + d + ") = " + x);
}
}
函数进行了更改,以最大限度地减少比较。例如,如果您在没有返回的情况下完成第一个findLargest
语句,则知道 if
与进一步比较无关,因为它小于至少一个其他值。