使用方法。全局声明变量

时间:2013-10-19 16:53:59

标签: java

我刚开始学习,我真的很努力让我的方法有效。我已经尝试全局声明变量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 

5 个答案:

答案 0 :(得分: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方法中使用它们。这就是为什么如果它们不是静态的,你就不能在静态方法中使用它们。

答案 1 :(得分: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 修饰符声明num1num2变量的原因是您无法访问非静态类变量来自 static 上下文(例如此处的getNumbers()方法)。

答案 2 :(得分:0)

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

答案 3 :(得分: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

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

答案 4 :(得分:0)

您需要在类中将变量num1和num2声明为静态,请检查此working sample

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
}