如何从另一个作用域调用变量?

时间:2019-03-13 16:48:23

标签: java scope

我正在学习Java。我来自python,因此作用域并不是最大的问题(至少对我来说是直截了当的)。我正在尝试制作一个简单的收据程序。我需要在收据末尾从方法ratingService()调用qualityOfService(我在需要的地方添加了注释)。我将如何去做呢?我读过其他有关范围的地方,但这并没有帮助。很抱歉,我的格式很糟糕,我知道阅读可怕的代码是多么令人讨厌。预先感谢。

import java.util.Scanner;

class Main 
    {
    public static void main(String[] args) 
    {
        Scanner userInput = new Scanner(System.in);

        System.out.println("Enter your first name: ");
        String firstName = userInput.nextLine();

        System.out.println("Enter the cost of your meal: ");
        double mealCost = userInput.nextDouble();
        String dummy = userInput.nextLine();

        System.out.println("Enter the percentage of tip you want to leave: ");
        double tipPercentage = userInput.nextDouble();
        String dummy1 = userInput.nextLine();

        System.out.println("Enter the quality of service: ");
        int qualityOfService = userInput.nextInt();

        // calculate values
        double tipAmount = (double)mealCost * ((double)tipPercentage/100);
        double totalCost = (double)mealCost + (double)tipAmount;
        int numberOfTwentyDollarBills = (int)totalCost / 20;   

        // 
        System.out.println("Thank you " + firstName + " for choosing our resturant!");
        System.out.println("=========================================");
        System.out.println("Cost of Meal = " + mealCost);
        System.out.println("Percentage of tip = " + tipPercentage);
        System.out.println("Tip amount = " + tipAmount);
        System.out.println("Total cost of meal = " + totalCost);
        System.out.println("Total number of twenty dollar bills = " + numberOfTwentyDollarBills);
        System.out.println("Rating of your service = " + qualityOfService);
        System.out.println("=========================================");
        // calling ratingService() goes here.
    }

    // service rating method 
    public static void ratingservice() 
    {
        int qualityOfService = 1;
        if (qualityOfService == 1) {
            System.out.println("We are sorry you weren't happy with your serivce. Please let staff know if you have any concerns");
        }
        else if (qualityOfService == 2) {
            System.out.println("We are sorry you weren't happy with your serivce. Please let staff know if you have any concerns");
        }
        else if (qualityOfService == 3) {
            System.out.println("We are happy that you enjoyed your stay somewhat, please let staff know if you have any concerns.");
        }
        else if (qualityOfService == 4) {
            System.out.println("We are glad you enjoyed your stay! Please let our staff know if you have any concerns");
        }
        else {
            System.out.println("We are glad you enjoyed your stay! Please let our staff know if you have any concerns");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您需要在qualityOfService方法上将ratingservice作为参数。

public static void ratingservice(int qualityOfService) {...}

然后在第一个方法中像这样调用它:

ratingService(qualityOfService)

这是因为在Java中,我们必须将方法所需的所有变量作为参数传递给方法。每个方法都有自己的变量范围,为了将变量从一个范围(第一个方法)传递到另一个范围(第二个方法),必须将其作为参数传递。

另一种解决方案是在您的类中创建一个实例变量,但是由于这些是静态方法,因此无法使用。一个实例变量可以被一个类的所有实例(非静态)方法使用。

答案 1 :(得分:0)

这是因为您在qualityOfService方法内声明 main(),因此该方法仅在该方法内可用,如果您希望在其他地方使用它,则必须被通过(这是“范围”的意思)。建议的解决方案的另一种选择是像这样在类级别进行设置:

class Main {

    static int qualityOfService;

    public static void main(String[] args) 
    {
    ...stuff...
    qualityOfService = userInput.nextInt();
    ...
    }

    public static void ratingservice() 
    {
        if (qualityOfService == 1) {
            System.out.println("We are sorry you weren't happy with your serivce. Please let staff know if you have any concerns");
        }
        ...etc...
    }
}

在这种情况下,因为您是在类级别进行了声明,即使您没有使用{em> em 初始化,除非您使用main()方法,可以在该类的任何(静态)方法中使用。