在 JOptionPane 中显示 if 语句
Display if statements within JOptionPane
有没有办法在 JOptionPane
中显示 if
语句?我尝试使用 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
在消息对话框中显示 window。
我的代码如下:
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);
}
}
如有任何帮助,我们将不胜感激。提前致谢。
而不是打印出你的 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
并参考这个post:
Customize JOptionPane Dialog
有关 JOptionPane 和自定义其外观的更多信息。
有没有办法在 JOptionPane
中显示 if
语句?我尝试使用 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
在消息对话框中显示 window。
我的代码如下:
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);
}
}
如有任何帮助,我们将不胜感激。提前致谢。
而不是打印出你的 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
并参考这个post: Customize JOptionPane Dialog
有关 JOptionPane 和自定义其外观的更多信息。