【发布时间】: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(最小部分)。删除与手头问题无关的部分代码使我们更容易快速阅读并了解您的问题到底是什么。