0

斜体粗体 ► 代码缩进 4 个空格 ► 反引号转义like _so_ ► 将 > 放在行首引用 ► 建立链接

4

6 回答 6

0

因为int[] num仅在主函数中定义,但在您的其他函数中没有定义,它不是类变量。

于 2013-08-27T04:30:52.730 回答
0

num变量的范围main()仅取决于方法。你必须public static在类中声明它才能从函数中使用

第二种方法是在函数中将数组作为参数传递。

于 2013-08-27T04:33:03.857 回答
0

导致和方法int[] num不可见。sumNums()reverseNums()

public static void main(String[] args) 
{
...

System.out.println("The Sum of the numbers is " + sumNums(num));

...

}

public static int sumNums(int[] num)
{  
return num[0] + num[1] + num[2];
}
于 2013-08-27T04:34:15.457 回答
0

您可以使 int[] num 类变量或将 int[] num 作为参数传递给方法并更改方法签名。

static int[] num = new int[3];
public static void main(String[] args) 
{
//call for input
System.out.println("Please Enter a 3-digit number..");
Scanner in = new Scanner(System.in);
for(int i = 0; i < num.length; i++){
int val = in.nextInt();
num[i] = val;
}

System.out.println("The Sum of the numbers is " + sumNums());
System.out.println("The Reverse of the numbers is " + reverseNums());

}

public static int sumNums()
{  
return num[0] + num[1] + num[2];
}

public static int reverseNums()
{
return num[2] + num[1] + num[0]; 
} 

}

或者

public static void main(String[] args) 
{
//call for input
System.out.println("Please Enter a 3-digit number..");
Scanner in = new Scanner(System.in);
int[] num = new int[3];
for(int i = 0; i < num.length; i++){
int val = in.nextInt();
num[i] = val;
}

System.out.println("The Sum of the numbers is " + sumNums(num));
System.out.println("The Reverse of the numbers is " + reverseNums(num));

}

public static int sumNums(in[] num)
{  
return num[0] + num[1] + num[2];
}

public static int reverseNums(int[] num)
{
return num[2] + num[1] + num[0]; 
}  

}
于 2013-08-27T04:59:07.780 回答
0

static method只能使用static variables同一类的。在您的情况下,您使用的变量不是static variable. 你可以这样写:

int[] num = new int[3];
public static void main(...) {
    //the code you want
    sumMums(num);
}

public static int sumNums(in[] num) {  
   return num[0] + num[1] + num[2];
}
于 2013-08-27T05:19:51.613 回答
0

num 变量的范围仅在 main 方法中。要么将其作为参数传递,要么使其成为全局变量。请参考java教程。

以 NUM 作为参数

package com;

import java.util.Scanner;


public class WorkSheet_1  {
    //static int[] num = new int[3];

    public static void main(String[] args) 
    {
        //call for input
        System.out.println("Please Enter a 3-digit number..");
        Scanner in = new Scanner(System.in);
        int[] num = new int[3];
        for(int i = 0; i < num.length; i++){
        int val = in.nextInt();
        num[i] = val;
        }

        System.out.println("The Sum of the numbers is " + sumNums(num));
        System.out.println("The Reverse of the numbers is " + reverseNums(num));

    }



public static int sumNums(int[] num)
{  
    return num[0] + num[1] + num[2];
}

public static int reverseNums(int[] num)
{
    return num[2] + num[1] + num[0]; 
}  

}

以 NUM 作为全局(静态)

package com;

import java.util.Scanner;


public class WorkSheet_1  {
    static int[] num = new int[3];

    public static void main(String[] args) 
    {
        //call for input
        System.out.println("Please Enter a 3-digit number..");
        Scanner in = new Scanner(System.in);

        for(int i = 0; i < num.length; i++){
        int val = in.nextInt();
        num[i] = val;
        }

        System.out.println("The Sum of the numbers is " + sumNums());
        System.out.println("The Reverse of the numbers is " + reverseNums());

    }



public static int sumNums()
{  
    return num[0] + num[1] + num[2];
}

public static int reverseNums()
{
    return num[2] + num[1] + num[0]; 
}  

}
于 2013-08-27T05:25:29.533 回答