0

我刚刚开始学习,我真的很难让我的方法奏效。我尝试全局声明变量 num1 和 num2 ,但没有任何效果。我究竟做错了什么?

import java.util.*;

public class MinMax 
{
    static Scanner keyboard = new Scanner(System.in);

    //a re-useable method to get the numbers
    public static void getNumbers()
    {
        int num1, num2;
        System.out.println("Please enter number 1");
        num1 = keyboard.nextInt();
        System.out.println("Please enter number 2");
        num2 = keyboard.nextInt();
    }
    //getNumbers

    public static void printAnswer(int pNum1, int pNum2)
    {
        if (pNum1 > pNum2)
        {
            System.out.println("Number 1 is the max number");
        }
        else
        {
            System.out.println("Number 2 is the max number");
        }
    }//printAnswer

    public static void main(String [] args)
    {  
        getNumbers();
        printAnswer(num1, num2);
        getNumbers();
        printAnswer(num1, num2);

    }//main
}//class 
4

5 回答 5

1
static Scanner keyboard = new Scanner(System.in);
static int num1, num2; // I think you tried without the static keyword, and that's why it didnt work

您需要将它们设为静态,就在Scanner声明的下方,因为就像 Scanner 对象一样,您在static方法中使用它们。这就是为什么如果它们不是静态的,你就不能在静态方法中使用它们。

于 2013-10-19T16:56:05.523 回答
0

不,您没有 num1, num2在全球范围内声明。您已在getNumbers()此函数本地的函数中声明它。您需要在类的上下文中声明它,因为您已经声明了它的KeyBoard实例Scanner以及它的正下方(或上方)。例如:

public class MinMax
{   
    static Scanner keyboard = new Scanner(System.in);
    static int num1, num2;

//a re-useable method to get the numbers
public static void getNumbers()
{

    System.out.println("Please enter number 1");
    num1 = keyboard.nextInt();
    System.out.println("Please enter number 2");
    num2 = keyboard.nextInt();
}

//........... your other code
}

必须使用static修饰符声明num1andnum2变量的原因是您无法从静态上下文(例如这里的方法)访问非静态类变量getNumbers()

于 2013-10-19T16:56:00.890 回答
0

将 num1 和 num2 声明为类属性;您已将它们声明为局部变量。

于 2013-10-19T16:57:20.743 回答
0
import java.util.*;

public class MinMax
{   static Scanner keyboard = new Scanner(System.in);
    static int num1, num2;

//a re-useable method to get the numbers
public static void getNumbers()
{    
    System.out.println("Please enter number 1");
    num1 = keyboard.nextInt();
    System.out.println("Please enter number 2");
    num2 = keyboard.nextInt();
}//getNumbers

public static void printAnswer(int pNum1, int pNum2)
{
    if (pNum1 > pNum2)
    {
        System.out.println("Number 1 is the max number");
    }
        else
        {
            System.out.println("Number 2 is the max number");
        }
}//printAnswer

public static void main(String [] args)
{  
    getNumbers();
    printAnswer(num1, num2);
    getNumbers();
    printAnswer(num1, num2);

}//main
}//class

全局变量在您的类中声明,而不是在方法中。另外,请注意,您必须将变量设为静态,因为您在静态方法中使用它们。

于 2013-10-19T16:57:58.790 回答
0

您需要在您的类中将变量 num1 和 num2 声明为静态,检查此工作示例

class Ideone
{
static Scanner keyboard = new Scanner(System.in);

   //declare it as class variables
    static int num1;
    static int num2;

    //a re-useable method to get the numbers
    public static void getNumbers()
    {
        // int num1, num2; this declare two locals variables
        System.out.println("Please enter number 1");
        num1 = keyboard.nextInt();
        System.out.println("Please enter number 2");
        num2 = keyboard.nextInt();
    }
    //getNumbers

    public static void printAnswer(int pNum1, int pNum2)
    {
        if (pNum1 > pNum2)
        {
            System.out.println("Number 1 is the max number");
        }
        else
        {
            System.out.println("Number 2 is the max number");
        }
    }//printAnswer

    public static void main(String [] args)
    {  
        getNumbers();
        printAnswer(num1, num2);
        getNumbers();
        printAnswer(num1, num2);

    }//main
}
于 2013-10-19T17:00:39.633 回答