いくつかの助けが必要です、それは計算を計算し、望ましい値を返す必要があります。代わりにゼロを返します。ここの最初のコードは計算を実行しません。実行時エラー
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;
}
}
/////////////////////////////////////// TESTER CLASS ///////// /////////////////////////
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'にはメンバ変数を使用しないでください。メソッドパラメータとローカル変数としてそれぞれ宣言します。 –
'TipCalc'クラスのコンストラクタに渡している' satisfactionRating'に値を代入していません。この '公共のTipCalcを試してください(int satisfactionRating){ this.satisfactionRating = satisfactionRating; } ' –