1

我已经尝试了网站上已经给出的各种建议。但老实说,我找不到解决问题的方法。

基本上,当我尝试使用在 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);
            }
        }
4

5 回答 5

3

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

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

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

 name = input.next();

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

于 2012-11-12T11:06:03.493 回答
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:-

于 2012-11-12T11:04:46.343 回答
1

您不能在该方法之外使用局部变量(在您的方法中定义的变量) 。他们只限于那个方法。您要么必须使其成为实例变量,要么返回该变量,以便其他方法可以使用它。在你的情况下。如果你想使用sal外部方法 accept 。使其成为实例变量。

Class classname {
public long sal;

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

public void display(){
//cvan access sal here
}
}
于 2012-11-12T11:05:00.627 回答
1

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

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

对于选项 2,您可以这样定义一个返回对象:

class Teacher {
    private String name;
    private String phone;

    // add appropriate constructor
}

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

于 2012-11-12T11:07:10.713 回答
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);
        }
  }
于 2012-11-12T11:16:47.143 回答