我正在编写一个非常简单的Java程序,但在运行时提取错误的信息。我知道它很小,但需要一套新的眼睛。请看一下,并提前感谢您。
我到处搜寻,这是我的最后一步。
package example;
import java.util.Scanner;
public class CustomersChange {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
final double MONEY = 100;
// Receive the amount
System.out.print("Enter the amount of your purchase in whole dollars, for example 96. "
+ "You have $100 to spend: ");
double amount = input.nextDouble();
int changeLeft = (int)(amount * 100);
// Find the number of twenties left
int numberOfTwenties = changeLeft / 2000;
changeLeft = changeLeft % 2000;
// Find the number of tens left
int numberOfTens = changeLeft / 1000;
changeLeft = changeLeft % 1000;
// Find the number of fives left
int numberOfFives = changeLeft / 500;
changeLeft = changeLeft % 500;
// Find the number of ones in the left
int numberOfDollars = changeLeft / 100;
changeLeft = changeLeft % 100;
// Display results
System.out.println("Your change is " + (MONEY - amount) + " dollars.");
System.out.println("Your change of " + (MONEY - amount) + " dollars has:");
System.out.println(" " + numberOfTwenties + " twenties ");
System.out.println(" " + numberOfTens + " tens");
System.out.println(" " + numberOfFives + " fives");
System.out.println(" " + numberOfDollars + " dollars");
}
}
答案 0 :(得分:1)
更改
int changeLeft = (int)(amount * 100);
要
int changeLeft = (int)(MONEY - amount)*100;