Есть ли у кого-нибудь идея, где моя проблема с выпуском?Weird Java Outputs - Запуск VM на Mac lion с Eclipse
Я написал программу на Java и запустил ее на виртуальной машине на Mac lion с Eclipse. Вместо того, чтобы получать 1.41
, я получил 1.4100000000000001
на своей машине.
Example:
Enter the number of quarter: 4
Enter the number of dimes: 3
Enter the number of nickels: 2
Enter the number of pennies: 1
Total $1.4100000000000001
Example:
Enter the number of quarter: 3
Enter the number of dimes: 2
Enter the number of nickels: 1
Enter the number of pennies: 6
Total $1.06
Example:
Enter the number of quarter: 5
Enter the number of dimes: 7
Enter the number of nickels: 4
Enter the number of pennies: 4
Total $2.1900000000000004
Иногда кажется, что выходы правильные, в то время как другие проблемы имеют проблемы.
Код:
import java.util.*;
public class CountChange
{
public static void main(String[]args)
{
Scanner inputScanner = new Scanner(System.in);
System.out.print("Enter the number of quarters: ");
int quarters = inputScanner.nextInt();
System.out.print("Enter the number of dimes: ");
int dimes = inputScanner.nextInt();
System.out.print("Enter the number of nickles: ");
int nickles = inputScanner.nextInt();
System.out.print("Enter the number of pennies: ");
int pennies = inputScanner.nextInt();
inputScanner.close();
double total =0.00;
total = quarters * 0.25 + dimes * 0.1 + nickles * 0.05 + pennies * 0.01;
System.out.println("Total $" + total);
System.out.print("Thank you");
}
}
http://floating-point-gui.de/ –
Если вы используете числа с плавающей запятой, вам нужно округлить результаты. Попробуйте 'System.out.printf (« $%.2f% n ", value);' –