java - 我需要帮助存储和显示二维数组中的信息

标签 java arrays

我需要创建一个程序,允许用户从学校列表中进行选择来模拟筹款 Activity 。然后总结了按学校和捐款金额估算的筹款情况。所有总计应存储在二维数组中。处理完所有学校的预计筹款额后,应以表格形式显示结果,每一列代表一所学校,每一行代表一笔捐款金额。

我已经把大部分程序都写下来了。现在它会计算用户选择的捐赠金额与选定的学校人口。我想要的帮助是将这些信息存储在二维数组中,然后显示它。我只是不确定我将如何去做。

这是我到目前为止所得到的:

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):(所需位置由“<---”表示)

 _____ 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     

最佳答案

   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];

关于java - 我需要帮助存储和显示二维数组中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29306000/

相关文章:

java - 错误: Type mismatch: cannot convert from HSSFWorkbook to Workbook

java - JAVA中通知是如何承载的?

java - 如何强制在每个测试方法中运行静态 block ?

android - For 语句仅在使用 Intent 传递数据时返回最后一个数据

c - C语言中如何统计数组中的元素个数

java - jvm中java热点类的加载

java - 调整大小时 jpanel 与 jdesktop Pane 不匹配

arrays - 如何有效地查找数组的所有可能子数组?

c - 如何将命令行参数中的数字解析为两个数组

java - 带轴 JTable