【问题标题】:How to call a variable from another scope?如何从另一个范围调用变量?
【发布时间】:2019-03-13 16:48:22
【问题描述】:

我正在学习 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");
        }
    }
}

【问题讨论】:

  • 传递变量在 Python 和 Java 之间并没有什么不同。
  • 不确定您的问题。为什么 ratingservice 不将 qualityOfService 作为 int 返回,而是 void 呢?另外,请检查MCVE(最小部分)。删除与手头问题无关的部分代码使我们更容易快速阅读并了解您的问题到底是什么。

标签: java scope


【解决方案1】:

您需要在ratingservice 方法上设置qualityOfService 参数。

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

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

ratingService(qualityOfService)

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

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

【讨论】:

  • 非常感谢!有用!你能解释一下它的语法吗?我不完全理解为什么必须这样做。
  • 感谢您的编辑。现在一切都说得通了。再次感谢!
  • 很高兴能帮上忙! :)
【解决方案2】:

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

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...
    }
}

在这种情况下,因为您在类级别声明它,即使直到您在 main() 方法中才初始化它,它也是可在类的任何(静态)方法中使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多