我需要创建一个程序,允许用户从学校列表中进行选择以模拟筹款活动。然后,它总结了学校和捐赠金额的估计筹款。所有总数应存储在二维数组中。在处理了所有学校的预算筹款后,它应以表格形式显示结果,每列代表一所学校,每行代表一个捐赠金额。
我已经完成了大部分程序。它现在计算用户选择的捐赠金额与选定的学校人口。我想要帮助的是将该信息存储在2D阵列中然后显示它。我只是不确定我会怎么做。
这是我到目前为止所得到的:
package u2a2a1;
import javax.swing.*;
public class U2A2A1 {
public static void main(String[] args) {
// DeclareVariables
String DonationChoice, SchoolChoice = "", SchoolPopulation;
double DonationTotals[][] = new double[4][9];
double Donation = 0;
// Initialize the DonationTotals array to zeros
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 9; j++) {
DonationTotals[i][j] = 0;
}
}
// Create menu
while (!SchoolChoice.equals("8")) {
SchoolChoice =
JOptionPane.showInputDialog("Which School is Fundraising?\n"
+ "0 - Catholic Central"
+ "\n1 - Holy Cross\n2 - John Paul II\n3 - Mother Teresa"
+ "\n4 - Regina Mundi\n5 - St Joeseph\n6 - St Mary"
+ "\n7 - St Thomas Aquinas\n8 - Exit\n");
// Ask user how much each student is donating
DonationChoice =
JOptionPane.showInputDialog("What is the donation amount?\n "
+ "0 - 25cents\n1 - 50cents\n2 - 1dollar\n3 - 2dollars");
if (DonationChoice.equals("0")) {
Donation = .25;
}
if (DonationChoice.equals("1")) {
Donation = 0.5;
}
if (DonationChoice.equals("2")) {
Donation = 1;
}
if (DonationChoice.equals("3")) {
Donation = 2;
}
// Ask user for the polulation of the school
SchoolPopulation = JOptionPane.showInputDialog("What is the population?");
Integer Population = Integer.valueOf(SchoolPopulation);
// Calculate AND Display
System.out
.println("================================================================================================\n");
System.out
.print(" CathCen HolyC JP II MotherT ReginaM St.Joe St.Tom St.Mary TOTAL \n\n");
for (int row = 0; row < 4; row++) {
System.out.print((row + 1) + "\t");
for (int col = 0; col < 9; col++) {
System.out.print(DonationTotals[row][col]);
// Print 1 tab if # is >999, 2 tabs otherwise
if (DonationTotals[row][col] > 999) {
System.out.print("\t");
} else {
System.out.print(" \t");
}
}
System.out.print("\n");
}
System.out.println("\n" + Donation * Population);
}
}
}
以下是我希望程序输出的内容。 例如,如果用户选择“天主教中心”捐赠金额为0.25且学校人口为2000,我希望程序在此处显示计算(250):(期望的位置由“&lt; ---”表示)< / p>
_____ CathCen HolyC JP II MotherT ReginaM St.Joe St.Tom St.Mary TOTAL
0.25 0.0<--- 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.50 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2.00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
答案 0 :(得分:0)
您应该将2D数组更改为数组[学校] [捐赠]。我现在相信[捐款] [学校],我认为这对你的目的没有好处。
假设您进行了更改:
for (int i = 0; i < array.length; i++) { // for each school
System.out.print("School name ");
for (int j = 0; j < array[i].length; j++) { // print the 4
// donation amounts in one line
System.out.print(array[i][j]+ " ");
}
System.out.println(""); // move to next line and school
}
你需要知道9个阵列中的哪个学校(i
)。
编辑:对象方法
public class School {
String name;
int[] donations;
public School(String name) {
this.name = name;
// maybe add something for donations if you want
}
// getters and setters for both fields
}
将这些对象添加到ArrayList
:
List<School> schools = new ArrayList<School>;
循环浏览列表,为每所学校添加/更改捐款。
这样可以避免学校名称的2D数组+数组的痛苦。
我认为这种方法比数组更适合您的问题。它将数据与一所学校联系起来,这使得以后更容易修改和添加学校。
答案 1 :(得分:0)
for (int row = 1; row <= 4; row++) {
System.out.print((row + 1) + "\t");
for (int col = 1; col <= 9; col++) {
setnumber(donations[row][col]);
}
System.out.println("total donation : " Donation * Population)
// call setFunctions
setNumber (donations[row][col])
DonationTotals[row][col] =donations[row][col];