【发布时间】:2016-10-25 03:56:47
【问题描述】:
需要一些帮助,它应该计算计算并返回所需的值。相反,它返回一个零。
public class TipCalc {
int satisfactionRating;
double tip;
public TipCalc(int satisfactionRating) {
}
public double calcTip(double bill) {
if (satisfactionRating==1) {
tip = bill*0.20;
}
if (satisfactionRating==2) {
tip = bill*0.15;
}
if (satisfactionRating==3) {
tip = bill*0.10;
}
return tip;
}
}
///////////////////////////////////////////////////////////////////////////////////////////// //////////////////////p>
import java.util.Scanner;
public class tiptester {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please put in the total cost of your meal ");
double mealPrice = input.nextFloat();
System.out.println("Please input your satisfaction rating ");
int satisfaction = input.nextInt();
TipCalc meal = new TipCalc(satisfaction);
double tipz = meal.calcTip(mealPrice);
double totalMeal = tipz+mealPrice;
System.out.println("The tip is " + tipz);
System.out.println("The cost of the meal is " + mealPrice);
System.out.println("The total cost is " + totalMeal);
}
}
【问题讨论】:
-
你有什么意见?
-
注意:不要对
int satisfactionRating和double tip使用成员变量。分别声明为方法参数和局部变量。 -
您没有将值分配给
satisfactionRating,而是将其传递给TipCalc类构造函数。试试这个public TipCalc(int satisfactionRating) { this.satisfactionRating = satisfactionRating; }