gpt4 book ai didi

java - 将数组的输出格式化为列

转载 作者:行者123 更新时间:2023-12-01 14:41:10 25 4
gpt4 key购买 nike

我只是需要一些帮助来将输出格式化为特定的方式。我满足了作业的所有要求,除了输出。

对于这篇文章的错误格式,我深表歉意,非常感谢任何建议和帮助。

我需要输出如下:

Mr. Random - $500
Mr. BobRandom - $250
etc-8 more times
(sum total of all dollars)

这是我的代码:

import javax.swing.JOptionPane;
import java.util.Arrays;

public class PeerTutors {
public static void main(String[] args) {
String[] myNameArray = new String[2];
String[] myGradeArray = new String[2];
double[] myStudentArray = new double[2];
double[] stipend = new double[2];
int sum = 0;

for (int i = 0; i < 2; i++) {
myNameArray[i] = (JOptionPane
.showInputDialog(null,
"Enter the Peer Tutor name, last name then followed by the first name."));

}
Arrays.sort(myNameArray, String.CASE_INSENSITIVE_ORDER);
JOptionPane.showMessageDialog(null, "The Peer Tutors Names are :"
+ Arrays.toString(myNameArray));

for (int a = 0; a < 2; a++) {
myGradeArray[a] = JOptionPane.showInputDialog(null,
"Please enter the highest degree earned for: "
+ myNameArray[a]);
}
JOptionPane.showMessageDialog(null, "The Peer Tutors Names are :"
+ Arrays.toString(myNameArray) + Arrays.toString(myGradeArray));

for (int b = 0; b < 2; b++) {
myStudentArray[b] = Double.parseDouble(JOptionPane.showInputDialog(
null, "Please enter the number of students"
+ myNameArray[b] + " has assisted"));
}
JOptionPane.showMessageDialog(null,
"The Peer Tutors Report: " + Arrays.toString(myNameArray)
+ "with" + Arrays.toString(myStudentArray)
+ " of children tutoring");

for (int c = 0; c < 2; c++) {
if (myGradeArray[c].equals("BS")) {
stipend[c] = 9.5 * myStudentArray[c];

} else if (myGradeArray[c].equals("MS")) {
stipend[c] = 15 * myStudentArray[c];

} else {
stipend[c] = 20 * myStudentArray[c];
}
}
for (double d : stipend) {
sum += d;
}

for (int d = 0; d < 2; d++) {
JOptionPane.showMessageDialog(null, myNameArray[d] + "-"
+ stipend[d] + "\n");

}
}
}

最佳答案

基本上,对于每一列,您都需要某种类型的列表(数组或最好是List)。您还需要维护每列最大宽度的信息(我通常将其放在另一个 List 中)。

您需要循环每一行,分解为它的列元素,并将每个元素放置在相应的列List中。您还应该计算每列的宽度要求。

一旦你把这一切都结构化了,你需要重建输出,使用像StringBuilder这样的东西来重建输出的每一行,但这样每一列都有所需的额外间距让色谱柱流动。

已更新示例

现在,通常,我会将每一行存储为单独的对象,因为这样可以更轻松地保持行总数的一致想法,但是,使用您的代码作为起点...

import java.text.NumberFormat;

public class TestTextColumnFormatting {

public static void main(String[] args) {
String[] myNameArray = new String[]{
"Mr. Samete",
"Ms. Torbura",
"Mr. Perem",
"Mr. Rothaw",
"Miss. Endemos",
"Mr. Chomoshon",
"Mrs. Aldir",};
double[] stipends = new double[]{
500,
200,
300,
1000,
250,
125,
600,
338
};
NumberFormat nf = NumberFormat.getCurrencyInstance();
double tally = 0;

int columnWidths[] = new int[2];
for (int index = 0; index < myNameArray.length; index++) {
columnWidths[0] = Math.max(columnWidths[0], myNameArray[index].length());
columnWidths[1] = Math.max(columnWidths[1], nf.format(stipends[index]).length());
tally += stipends[index];
}

columnWidths[1] = Math.max(columnWidths[1], nf.format(tally).length());

StringBuilder sb = new StringBuilder(128);
for (int index = 0; index < myNameArray.length; index++) {

String name = myNameArray[index];
String stipend = nf.format(stipends[index]);

sb.append(name).append(fill(name, columnWidths[0], ' ')).append(" ");
sb.append(fill(stipend, columnWidths[1], ' ')).append(stipend);
sb.append("\n");

}


sb.append(fill("", columnWidths[0], ' ')).append(" ");
sb.append(fill("", columnWidths[1], '=')).append("\n");
sb.append(fill("", columnWidths[0], ' ')).append(" ");
sb.append(fill(nf.format(tally), columnWidths[1], ' ')).append(nf.format(tally));
sb.append("\n");

System.out.println(sb.toString());

}

public static String fill(String sValue, int iMinLength, char with) {

StringBuilder filled = new StringBuilder(iMinLength);

while (filled.length() < iMinLength - sValue.length()) {

filled.append(with);

}

return filled.toString();

}
}

这将输出类似...的内容

Mr. Samete      $500.00
Ms. Torbura $200.00
Mr. Perem $300.00
Mr. Rothaw $1,000.00
Miss. Endemos $250.00
Mr. Chomoshon $125.00
Mrs. Aldir $600.00
=========
$2,975.00

已更新

我真是个白痴,我刚刚意识到你正在使用 JOptionPane :P

最简单的解决方案是构建一个 HTML 表并让 Swing 为您呈现它...

enter image description here

import java.text.NumberFormat;
import javax.swing.JOptionPane;

public class TestTextColumnFormatting {

public static void main(String[] args) {
String[] myNameArray = new String[]{
"Mr. Samete",
"Ms. Torbura",
"Mr. Perem",
"Mr. Rothaw",
"Miss. Endemos",
"Mr. Chomoshon",
"Mrs. Aldir",};
double[] stipends = new double[]{
500,
200,
300,
1000,
250,
125,
600,
338
};
NumberFormat nf = NumberFormat.getCurrencyInstance();
double tally = 0;

StringBuilder sb = new StringBuilder(128);
sb.append("<html><table border='0'>");
for (int index = 0; index < myNameArray.length; index++) {

String name = myNameArray[index];
String stipend = nf.format(stipends[index]);

sb.append("<tr><td align='left'>");
sb.append(name);
sb.append("</td><td align='right'>");
sb.append(stipend);
sb.append("</td></tr>");

tally += stipends[index];

}
sb.append("<tr><td align='center'>");
sb.append("</td><td align='right'>");
sb.append(nf.format(tally));
sb.append("</td></tr>");

JOptionPane.showMessageDialog(null, sb.toString());

}
}

关于java - 将数组的输出格式化为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15940485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com