有没有办法在if
中显示JOptionPane
个陈述?我尝试使用String.format
并尝试将if
语句封装在方法中,然后使用JOptionPane
调用该方法,但没有任何效果。我无法弄清楚我做错了什么。
我想要类似于下面的控制台显示器
--- $ 1,042.18细分如下---
10 hundred dollar bills
2 twenty dollar bills
2 one dollar bills
1 dime
1 nickel
3 pennies
显示在消息对话窗口中。
我的代码如下:
import javax.swing.JOptionPane;
public class ChangeDisplayJOptionPane {
public static void main(String[] args) {
double amount, originalAmount;
int hundredDollars, fiftyDollars, twentyDollars, tenDollars,
fiveDollars, oneDollars, quarters, dimes, nickels, pennies;
String amountString = JOptionPane.showInputDialog("Enter an amount:");
originalAmount = Double.parseDouble(amountString);
amount = originalAmount;
amount *= 100;
hundredDollars = (int)amount / 10_000;
amount %= 10_000;
fiftyDollars = (int)amount / 5_000;
amount %= 5_000;
twentyDollars = (int)amount / 2_000;
amount %= 2_000;
tenDollars = (int)amount / 1_000;
amount %= 1_000;
fiveDollars = (int)amount / 500;
amount %= 500;
oneDollars = (int)amount / 100;
amount %= 100;
quarters = (int)amount / 25;
amount %= 25;
dimes = (int)amount / 10;
amount %= 10;
nickels = (int)amount / 5;
amount %= 5;
pennies = (int)amount;
JOptionPane.showMessageDialog(null, String.format("%n--- $%,.2f is broken down as follows ---%n%n",
originalAmount));
// Must display in dialog window
System.out.printf("%n--- $%,.2f is broken down as follows ---%n%n",
originalAmount);
if (hundredDollars == 1)
System.out.printf("%10s %-20s%n",hundredDollars,"hundred dollar bill");
else if (hundredDollars > 1)
System.out.printf("%10s %-20s%n",hundredDollars,"hundred dollar bills");
if (fiftyDollars == 1)
System.out.printf("%10s %-20s%n",fiftyDollars,"fifty dollar bill");
if (twentyDollars == 1)
System.out.printf("%10s %-20s%n",twentyDollars,"twenty dollar bill");
else if (twentyDollars > 1)
System.out.printf("%10s %-20s%n",twentyDollars,"twenty dollar bills");
if (tenDollars == 1)
System.out.printf("%10s %-20s%n",tenDollars,"ten dollar bill");
if (fiveDollars == 1)
System.out.printf("%10s %-20s%n",fiveDollars,"five dollar bill");
if (oneDollars == 1)
System.out.printf("%10s %-20s%n",oneDollars,"one dollar bill");
else if (oneDollars > 1)
System.out.printf("%10s %-20s%n",oneDollars,"one dollar bills");
if (quarters == 1)
System.out.printf("%10s %-20s%n",quarters,"quarter");
else if (quarters > 1)
System.out.printf("%10s %-20s%n",quarters,"quarters");
if (dimes == 1)
System.out.printf("%10s %-20s%n",dimes,"dime");
else if (dimes > 1)
System.out.printf("%10s %-20s%n",dimes,"dimes");
if (nickels == 1)
System.out.printf("%10s %-20s%n",nickels,"nickel");
if (pennies == 1)
System.out.printf("%10s %-20s%n",pennies,"penny");
else if (pennies > 1)
System.out.printf("%10s %-20s%n",pennies,"pennies");
System.exit(0);
}
}
任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:1)
而不是打印if中的每个语句,就像你在这里一样:
if (hundredDollars == 1)
System.out.printf("%10s %-20s%n",hundredDollars,"hundred dollar bill");
else if (hundredDollars > 1)
System.out.printf("%10s %-20s%n",hundredDollars,"hundred dollar bills");
if (fiftyDollars == 1)
System.out.printf("%10s %-20s%n",fiftyDollars,"fifty dollar bill");
您将构建一个字符串,然后将该字符串放在JOptionPane中,如下所示:
String str = "";
if (hundredDollars == 1)
str += String.format(%10s %-20s%n",hundredDollars,"hundred dollar bill");
else if (hundredDollars > 1)
str += String.format(%10s %-20s%n",hundredDollars,"hundred dollar bills");
JOptionPane.showMessageDialog(null, str, originalAmount, JOptionPane.INFORMATION_MESSAGE));
请参阅此处的示例: https://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html
参考这篇文章: Customize JOptionPane Dialog
有关JOptionPane的更多信息并自定义其外观。