我想知道在这种情况下使用String.format的最佳方式...
String names[] = new String[10];
int idNumber[] = new int[10];
String list = "";
for( byte pos = 0; pos < idNumber.length; pos++){
names[pos] = JOptionPane.showInputDialog("Enter the name...");
idNumber[pos] = Integer.parseInt(JOptionPane.showInputDialog("Enther the ID number..."));
list += ("Name: " + names[pos] + "ID: " + idNumber + "\n");
}
JOptionPane.showMessagedialog(null, list);我希望使用String.format来更改"list“的输出
发自:
Name: John Wilson ID: 56
Name: Edward Sinclair ID: 60
Name: Bob Stuart ID: 77至:
Name: John Wilson ID: 56
Name: Edward Sinclair ID: 60
Name: Bob Stuart ID: 77如何正确使用%-10s...%-5s...在这种情况下?我有点迷路了..。提前谢谢。
发布于 2012-08-27 06:45:10
我真的不明白你在使用string.format时有什么问题。你真的试过了吗?
list += ("Name: %20s ID: %02d\n").format(names[pos], idNumber[pos]);如前所述,更有效的方法是:
StringBuilder list = new StringBuilder();然后用list.append(...)替换list += ...
https://stackoverflow.com/questions/12134456
复制相似问题