如何初始化2d数组以及如何从java中的main方法访问它?

时间:2017-04-01 13:35:46

标签: java class constructor

每次循环继续时,创建的2d数组会覆盖先前创建的2d数组,如何创建单独的2d数组并按下面列出的方式访问/更改它。

我目前是一名高中学生,参加在线计算机科学课程,并希望通过编码提高我的效率,并获得一些关于我的坏习惯的提示。这是我的计划:

import java.util.Arrays;
import java.util.Scanner;

public class FundRaising 
{

    public static void main(String[] args) 
    {

        Scanner scanner = new Scanner(System.in);



        System.out.println("Would you like to input donation information regarding the selected schools?\n0 - Yes\n1 - No"); //runs the program or ends the program
        Integer loop = scanner.nextInt();

        while (loop == 0) //while the user would like to run the program, the code will look
            {
            if (loop == 0) //continues if the user would like to continue
                {
                System.out.println("Which school is fundraising?\n0 - Central\n1 - Holy Cross\n2 - John Paul II\n3 - Teresa\n4 - Regina\n5 - Joeseph\n6 - Mary\n7 - Thomas Aquinas");
                Integer school = scanner.nextInt(); //asks which school information will be input for

                System.out.println("Amount donated?\n0 - 25 cents\n1 - 50 cents\n2 - 1 dollar\n3 - 2 dollars");
                Integer donation = scanner.nextInt(); //asks the amount donated

                System.out.println("School population?");
                Integer population = scanner.nextInt(); //asks the population of the school





                if (donation == 0) //if the first donation amount is selected then this selection will be used
                    {
                    double schoolTotal = 0.25*population; //the donation amount times the population gives the total for the school selected
                    System.out.println("  Cen Cross Paul Trsa Rgna Joes Mary Tom  Amnt Total"); //prints out headers for the 2d array



                    int rows = 4; //set the initial size of the 2d array according to schools and donations
                    int columns = 10;
                    double totals [][] = new double [rows][columns]; //initialize the 2d array
                    totals[0][8] = 0.25;
                    totals[1][8] = 0.5; //set various positions on the 2d array to equal the donation amounts
                    totals[2][8] = 1.00;
                    totals[3][8] = 2.00;
                    totals[donation][school] = schoolTotal; //set the positions corredsponding to the input school and donation in order to create a "total" column on the 2d array
                    totals[donation][9] = totals[0][0]+totals[0][1]+totals[0][2]+totals[0][3]+totals[0][4]+totals[0][5]+totals[0][6]+totals[0][7]; //the sums of everything in the row is used to find a value for the total column


                    double finalDonations = totals[0][9]+totals[1][9]+totals[2][9]+totals[3][9]; //adds the total for each school in the total column to create a final total for donations


                    System.out.print(Arrays.deepToString(totals).replace("], ", "]\n ")); //prints out the 2d array with slight modifications for easier viewing
                    System.out.println("\nTotals donations:" + finalDonations +""); //prints the total amount of donations to the user




                    }





                if (donation == 1) //each subsequence if statement is used if the donation amount differs (4 different donations amounts, 25 cents, 50 cents, 1 dollar and 2 dollars
                    { //remainder of code is drycut paste with small modifications to adjust for differing donation amount
                    double schoolTotal = 0.50*population;
                    System.out.println("  Cen Cross Paul Trsa Rgna Joes Mary Tom  Amnt Total");



                    int rows = 4;
                    int columns = 10;
                    double totals [][] = new double [rows][columns];
                    totals[0][8] = 0.25;
                    totals[1][8] = 0.5;
                    totals[2][8] = 1.00;
                    totals[3][8] = 2.00;
                    totals[donation][school] = schoolTotal;
                    totals[donation][9] = totals[1][0]+totals[1][1]+totals[1][2]+totals[1][3]+totals[1][4]+totals[1][5]+totals[1][6]+totals[1][7];


                    double finalDonations = totals[0][9]+totals[1][9]+totals[2][9]+totals[3][9];


                    System.out.print(Arrays.deepToString(totals).replace("], ", "]\n "));
                    System.out.println("\nTotals donations:" + finalDonations +"");


                    }


                if (donation == 2)
                    {
                    double schoolTotal = 1.00*population;
                    System.out.println("  Cen Cross Paul Trsa Rgna Joes Mary Tom  Amnt Total");



                    int rows = 4;
                    int columns = 10;
                    double totals [][] = new double [rows][columns];
                    totals[0][8] = 0.25;
                    totals[1][8] = 0.5;
                    totals[2][8] = 1.00;
                    totals[3][8] = 2.00;
                    totals[donation][school] = schoolTotal;
                    totals[donation][9] = totals[2][0]+totals[2][1]+totals[2][2]+totals[2][3]+totals[2][4]+totals[2][5]+totals[2][6]+totals[2][7];


                    double finalDonations = totals[0][9]+totals[1][9]+totals[2][9]+totals[3][9];


                    System.out.print(Arrays.deepToString(totals).replace("], ", "]\n "));
                    System.out.println("\nTotals donations:" + finalDonations +"");


                    }


                if (donation == 3)
                    {
                    double schoolTotal = 2.00*population;
                    System.out.println("  Cen Cross Paul Trsa Rgna Joes Mary Tom  Amnt Total");



                    int rows = 4;
                    int columns = 10;
                    double totals [][] = new double [rows][columns];
                    totals[0][8] = 0.25;
                    totals[1][8] = 0.5;
                    totals[2][8] = 1.00;
                    totals[3][8] = 2.00;
                    totals[donation][school] = schoolTotal;
                    totals[donation][9] = totals[3][0]+totals[3][1]+totals[3][2]+totals[3][3]+totals[3][4]+totals[3][5]+totals[3][6]+totals[3][7];


                    double finalDonations = totals[0][9]+totals[1][9]+totals[2][9]+totals[3][9];


                    System.out.print(Arrays.deepToString(totals).replace("], ", "]\n "));
                    System.out.println("\nTotals donations:" + finalDonations +"");


                    }

                }




            else //if the user did not chose to run the program this message is displayed
                {
                        System.out.println("You did not not choose to input information");
                }

            }


    }
}

3 个答案:

答案 0 :(得分:1)

实际上,每次循环迭代都会创建新数组。

移动这部分

int rows = 4; //set the initial size of the 2d array according to schools and donations
int columns = 10;
double totals [][] = new double [rows][columns]; //initialize the 2d array

到主方法,就在Scanner scanner = new Scanner(System.in);之后,并从循环内部删除它,所以下次学校的捐赠值不会被覆盖。

然后,如果你想要捐款增加的总和,你应该改变

totals[donation][school] = schoolTotal;

totals[donation][school] = totals[donation][school] + schoolTotal;

或者保持原样。

答案 1 :(得分:0)

我真的不明白你的问题是什么,但我建议你开始使用for循环而不是代码重复。

这样的行:

totals[donation][9] = totals[0][0]+totals[0][1]+totals[0][2]+totals[0][3]+totals[0][4]+totals[0][5]+totals[0][6]+totals[0][7];

应替换为:

for(int i=0; i<8; i++) (or i<totals[0].length if you want to run on all the cells)
    totals[donation][9]+=totals[0][i];

答案 2 :(得分:0)

考虑使用Collections代替数组来操作这样的数据。例如。 Map<String, Integer>用于处理不同的捐赠来源和价值