-3

I started studying for an exam and doing some practice programs with methods, and my mind is coming to a blank currently. I would like to know how I can initialize n1, n2, n3, and n4. I set them to 0 but the return statement returned only 0s.

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);

    }
}
4

3 回答 3

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);

}

}
于 2014-12-11T01:58:41.000 回答
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;
}

为什么n主要还是 100,而不是 999。这是因为 Java 通过值传递所有内容。对于原始类型,传递的值是实际值本身。这意味着实际值的副本将被传递给该方法。制作了一份副本。因此n1(scope: someMethod ) 只是n(scope: main ) 的一个副本。更改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));
}
于 2014-12-11T01:50:22.840 回答
0

变量n1throughn4将在findLargest函数中设置,但只有它们的本地副本,更改永远不会“回显”回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语句而不返回,您知道n1与进一步比较无关,因为它至少小于一个其他值。

于 2014-12-11T02:03:25.393 回答