我正在完成一个学校项目(我的第一门 Java 类(class)),我被告知只能使用 JOptionPane
。
我需要使用JOptionPane
而不是System.out.println
来做这样的事情但是:
int[] den = {1000, 500, 100 ,50, 20, 10 ,5, 2, 1};
System.out.println("\nDENOMINATIONS: \n");
for (int i = 0; i < 9; i++) {
count = amount / den[i]; // counting number of den[i] notes
if (count != 0) { //printing that denomination if the count is not zero
System.out.println(den[i] + "\tx\t" + count + "\t= " + den[i] * count);
}
totalNotes = totalNotes + count; //finding the total number of notes
amount = amount%den[i]; //finding the remaining amount
}
System.out.println("--------------------------------");
System.out.println("TOTAL\t\t\t= " + copy);
System.out.println("--------------------------------");
System.out.println("Total Number of Notes\t= " + totalNotes);
打印出来的东西像这样漂亮:
DENOMINATIONS:
1000 x 14 = 14000
500 x 1 = 500
100 x 3 = 300
50 x 1 = 50
5 x 1 = 5
1 x 1 = 1
————————————–
TOTAL = 14856
————————————–
Total Number of Notes = 21
最佳答案
阅读JOptionPane API 。您可以将任何组件添加到选项 Pane 。 “message”参数可以是一个简单的字符串来显示文本,也可以是一个 Swing 组件,也可以是一个包含字符串和组件的数组,在这种情况下,多个组件将垂直添加到选项 Pane 中。
因此您可以将文本添加到 JTextArea。然后将文本区域添加到选项 Pane 。
您还需要将文本区域的字体更改为等宽字体,以便可以正确对齐文本。
关于java - 如何使用 JOptionPane 内部迭代数组并显示自定义消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27652189/