Java 如何从另一个作用域调用变量?
我正在学习Java。我来自python,所以scopes并不是最大的问题(至少对我来说这是直截了当的)。我正在尝试做一个简单的收据程序。我需要在我的回执末尾从ratingService()方法调用qualityOfService(我将注释放在我想要的地方)。我该怎么做呢?我在其他地方读过关于示波器的文章,但这并没有真正起到帮助作用。抱歉我的格式设置,我知道读写得很糟糕的代码有多烦人。提前谢谢Java 如何从另一个作用域调用变量?,java,scope,Java,Scope,我正在学习Java。我来自python,所以scopes并不是最大的问题(至少对我来说这是直截了当的)。我正在尝试做一个简单的收据程序。我需要在我的回执末尾从ratingService()方法调用qualityOfService(我将注释放在我想要的地方)。我该怎么做呢?我在其他地方读过关于示波器的文章,但这并没有真正起到帮助作用。抱歉我的格式设置,我知道读写得很糟糕的代码有多烦人。提前谢谢 import java.util.Scanner; class Main { pub
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");
}
}
}
您需要将
qualityOfService
作为ratingservice
方法的一个参数
public static void ratingservice(int qualityOfService) {...}
然后在第一个方法中调用它,如下所示:
ratingService(qualityOfService)
这是因为在Java中,我们必须将方法所需的所有变量作为参数传递给该方法。每个方法都有自己的变量范围,为了将变量从一个范围(第一个方法)传递到另一个范围(第二个方法),必须将其作为参数传递 另一种解决方案是在类中创建一个实例变量,但由于这些是静态方法,因此不起作用。一个实例变量可以被一个类的所有实例(非静态)方法使用。这是因为您在
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()
方法之前您不会初始化它,它也可以在类的任何(静态)方法中使用。非常感谢!它起作用了!你能解释一下它的语法吗?我不完全明白为什么要这样做。谢谢你的编辑。现在一切都有意义了。再次感谢!很高兴我能帮忙!:)传递一个变量在Python和Java之间并没有什么不同。不确定您的问题到底是什么。为什么ratingservice
不将qualityOfService
返回为int,而是void?另外,请检查(最小部分)。删除与当前问题无关的部分代码,使我们更容易快速阅读并理解您的问题到底是什么。