如何允许各种函数使用变量? java的

时间:2012-11-12 11:02:40

标签: java function bluej

我已尝试过在网站上提供的各种建议..但老实说,我找不到解决方法。

基本上当我尝试使用在Accept中创建的任何变量时,它不能用于其他功能。有一个简单的解决方案来解决这个问题吗(不改变主要代码)

import java.util.Scanner;
class x4Salary
    {
        Scanner input = new Scanner(System.in);
        public void accept()
        {
            System.out.println("Please input the name of the teacher");
            String name = input.next();
            System.out.println("Please input the address ");
            String adress = input.next();
            System.out.println("Please input the phone number");
            long num = input.nextLong();
            System.out.println("Please input the subject specialization");
            String subjectSpecialization = input.next();
            String subS = subjectSpecialization;
            System.out.println("Please input the Monthly Salary");
            long sal = input.nextLong();
            if(sal>175000)
            {
                tax();
            }
            display();
            double tax = 0.0;
        }
        public void tax()
        {
            System.out.println("Your current salary is : " + sal);
            tax = sal + ((5/sal)*100);
            System.out.println("The salary + Tax : " +tax);
            display();
        }
        public void display()
            {
                System.out.println("The name of the teacher : " + name);
                System.out.println("The Address of the teacher :" +adress);
                System.out.println("The Phone number of the Teacher: "+num);
                System.out.println("The Subject Specialization of the Teacher" + subS);
                System.out.println("The Monthly salary of the teacher" +sal + tax);
            }
        }

5 个答案:

答案 0 :(得分:3)

您可以将这些变量设为类成员

class x4Salary
{
    protected String name, address; //and so on

然后代替String name = input.next();

 name = input.next();

名称将在X4Salary的所有方法中可见

答案 1 :(得分:1)

您需要将这些变量设为instance个变量。

在方法中创建的变量,仅限于该方法范围。在该方法之外是不可见的。它将在调用方法时创建,然后当方法执行完毕时,该变量才有资格获得Garbage Collection

对于您拥有的任何块范围都是如此。在一个块中创建的变量在该块之外将不可见。

例如: -

{   // A block
    int num = 10;
}

System.out.println(num);  // Error. num not visible here.

因此,要在所有方法中访问变量,请将其声明为: -

public class Demo {
    private int num;  // Instance variable declaration. Outside every method.

    public void setNum(int num) { 
        this.num = num; 
    }

    public int getNum() { 
        return this.num;  
    }
}

因此,正如您所看到的,这两种方法都使用变量num: - setNumgetNum


转到本教程,开始使用classesobjectsmember declarations: -

答案 2 :(得分:1)

您无法在该方法之外使用局部变量(在方法中定义的变量)他们仅限于该方法。您必须将其设为实例变量或返回变量,以便其他方法可以使用它。 在你的情况下。 如果你想使用 sal 外部方法接受。使它成为一个实例变量。

Class classname {
public long sal;

public void accept(){
//can access sal here
 sal = input.nextLong();
} 

public void display(){
//cvan access sal here
}
}

答案 3 :(得分:1)

这些变量的范围仅适用于方法。如果您想保留输入的值,您的选项是:

  1. 在包含类中创建成员变量并填充它们。这些值将在该类的生命周期内可用
  2. 返回方法中的值。由于您有多个值,您可能希望返回包含这些值的对象
  3. 从该方法中调用另一个对象,并以这种方式传递数据。
  4. 对于选项2,您可以定义一个返回对象:

    class Teacher {
        private String name;
        private String phone;
    
        // add appropriate constructor
    }
    

    我怀疑Teacher是您应用程序中的关键对象,您可能希望将方法display()添加到Teacher类本身。 请记住,OO是关于创建对象并让他们为您做事,而不是自己处理离散相关变量

答案 4 :(得分:1)

将变量定义为类中的成员变量,然后可以在所有成员方法中访问它。

import java.util.Scanner;
class x4Salary
    {
        private double tax=0.0;
        private string name, adress, subS;
        private long num, sal;
        Scanner input = new Scanner(System.in);
        public void accept()
        {
            System.out.println("Please input the name of the teacher");
            name = input.next();
            System.out.println("Please input the address ");
            adress = input.next();
            System.out.println("Please input the phone number");
            num = input.nextLong();
            System.out.println("Please input the subject specialization");
            subS = input.next();
            System.out.println("Please input the Monthly Salary");
            sal = input.nextLong();
            if(sal>175000)
            {
                tax();
            }
            display();
        }
        public void tax()
        {
            System.out.println("Your current salary is : " + sal);
            tax = sal + ((5/sal)*100);
            System.out.println("The salary + Tax : " +tax);
            display();
        }
        public void display()
        {
             System.out.println("The name of the teacher : " + name);
             System.out.println("The Address of the teacher :" +adress);
             System.out.println("The Phone number of the Teacher: "+num);
             System.out.println("The Subject Specialization of the Teacher" + subS);
             System.out.println("The Monthly salary of the teacher" +sal + tax);
        }
  }