これは動作するコードですが、私はJavaで整数と倍数を掛けることについての完全な研究の後に疑問を抱いています。なぜコードの下のスニペットがエラーを出すのかまだ分かりません。助けてください?intに括弧を追加し、javaにdoubleを追加するとエラーが発生する理由は?
public class Arithmetic {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double mealCost = scan.nextDouble(); // original meal price
int tipPercent = scan.nextInt(); // tip percentage
int taxPercent = scan.nextInt(); // tax percentage
scan.close();
// Calculate Tax and Tip:
double tip = mealCost * tipPercent/100; //HERE IS MY PROBLEM
double tax = mealCost * taxPercent/100; //HERE IS MY PROBLEM
// cast the result of the rounding operation to an int and save it as totalCost
int totalCost = (int) Math.round(mealCost + tax + tip);
System.out.println("The total meal cost is " + totalCost + " dollars.");
}
}
この回答がより論理的で、上記とは異なる値を示していることを知っていますか?あなたの第1の例で
double tip = meal * (tipPercent/100);
double tax = meal * (taxPercent/100);