Guten Abend,Math.round runden keine Zahlen
Mein Code wird nicht abgerundet, und ich weiß nicht warum! Bitte helfen Sie!
import java.util.*;
import java.math.*;
public class Arithmetic {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int tipPercentage; // tip percentage
int taxPercentage; // tax percentage
int totalTip; // total tip
int totalTax; // total tax
double mealCost; // original meal price
double totalCost = 0.0; // total meal price
// Write your calculation code here.
mealCost = scan.nextDouble();
tipPercentage = scan.nextInt();
taxPercentage = scan.nextInt();
totalCost += mealCost;
totalCost += mealCost*tipPercentage/100;
totalCost += mealCost*taxPercentage/100;
// cast the result of the rounding operation to an int and save it as totalCost
mealCost=(int)Math.round(totalCost);
// Print your result
System.out.println (" The total meal cost is " + totalCost + " dollars. ");
}
}
Ihre Frage ist ein wenig vage, können Sie es bitte mit ein bisschen mehr Details bearbeiten, was das Problem ist –
Sie haben einen Tippfehler, aber der ganze Ansatz ist irreführend. Siehe meine Antwort im Duplikat nach dem Grund. Wenn Sie Dezimalstellen benötigen, müssen Sie eine Dezimalzahl verwenden, z. über 'BigDecimal' oder' DecimalFormat', und in jedem Fall sollten Sie 'BigDecimal' für Geld, nicht Fließkomma verwenden. – EJP