用小数点计算变化(可能是简单的解决方案)
Counting Change with decimal point (probably simple solution)
我有以下代码(我不是很懂电脑,所以要温和),用户应该能够输入单个硬币数量,程序会告诉用户他们在所述硬币中有多少钱金额。
import java.util.*;
public class Coins {
public static final Scanner CONSOLE = new Scanner(System.in);
public static void main(String[] args) {
System.out.println ("Lab 2 by Maria T Williams\n");
quarterAmount( );
dimeAmount( );
nickelAmount( );
pennyAmount( );
}
public static void quarterAmount( ) {
System.out.print("Enter the number of quarters: ");
int quarterNumber = CONSOLE.nextInt( );
double amount = quarterNumber * 0.25;
System.out.print(quarterNumber + " You have $" + amount);
System.out.println(" worth of quarters.");
}
public static void dimeAmount( ) {
System.out.print("Enter the number of dimes: ");
int dimeNumber = CONSOLE.nextInt( );
double amount = dimeNumber * .10;
System.out.print(dimeNumber + " You have $" + amount);
System.out.println(" worth of dimes.");
}
public static void nickelAmount( ) {
System.out.print("Enter the number of nickels: ");
int nickelNumber = CONSOLE.nextInt( );
double amount = nickelNumber * 0.05;
System.out.print(nickelNumber + " You have $" + amount);
System.out.println(" worth of nickels.");
}
public static void pennyAmount( ) {
System.out.print("Enter the number of pennies: ");
int pennyNumber = CONSOLE.nextInt( );
double amount = pennyNumber * 0.01;
System.out.print(pennyNumber + " You have $" + amount);
System.out.println(" worth of pennies.");
}
}
如果我输入的金额在 100 位的数字不是“0”,我没问题。但是,如果我输入,比如说,两个季度,我会得到“$0.5”而不是“$0.50”。
您可以使用 DecimalFormat
来强制按照您的喜好设置数字格式。例如:
DecimalFormat df = new DecimalFormat("#.00");
double amount = dimeNumber * .10;
System.out.print(dimeNumber + " You have $" + df.format(amount) + " worth of dimes.");
输出计算结果时,可以这样使用DecimalFormat
:
DecimalFormat f = new DecimalFormat("#0.00");
System.out.println(f.format(dimeNumber));
我有以下代码(我不是很懂电脑,所以要温和),用户应该能够输入单个硬币数量,程序会告诉用户他们在所述硬币中有多少钱金额。
import java.util.*;
public class Coins {
public static final Scanner CONSOLE = new Scanner(System.in);
public static void main(String[] args) {
System.out.println ("Lab 2 by Maria T Williams\n");
quarterAmount( );
dimeAmount( );
nickelAmount( );
pennyAmount( );
}
public static void quarterAmount( ) {
System.out.print("Enter the number of quarters: ");
int quarterNumber = CONSOLE.nextInt( );
double amount = quarterNumber * 0.25;
System.out.print(quarterNumber + " You have $" + amount);
System.out.println(" worth of quarters.");
}
public static void dimeAmount( ) {
System.out.print("Enter the number of dimes: ");
int dimeNumber = CONSOLE.nextInt( );
double amount = dimeNumber * .10;
System.out.print(dimeNumber + " You have $" + amount);
System.out.println(" worth of dimes.");
}
public static void nickelAmount( ) {
System.out.print("Enter the number of nickels: ");
int nickelNumber = CONSOLE.nextInt( );
double amount = nickelNumber * 0.05;
System.out.print(nickelNumber + " You have $" + amount);
System.out.println(" worth of nickels.");
}
public static void pennyAmount( ) {
System.out.print("Enter the number of pennies: ");
int pennyNumber = CONSOLE.nextInt( );
double amount = pennyNumber * 0.01;
System.out.print(pennyNumber + " You have $" + amount);
System.out.println(" worth of pennies.");
}
}
如果我输入的金额在 100 位的数字不是“0”,我没问题。但是,如果我输入,比如说,两个季度,我会得到“$0.5”而不是“$0.50”。
您可以使用 DecimalFormat
来强制按照您的喜好设置数字格式。例如:
DecimalFormat df = new DecimalFormat("#.00");
double amount = dimeNumber * .10;
System.out.print(dimeNumber + " You have $" + df.format(amount) + " worth of dimes.");
输出计算结果时,可以这样使用DecimalFormat
:
DecimalFormat f = new DecimalFormat("#0.00");
System.out.println(f.format(dimeNumber));