在String类型的2D数组中添加列和行?

时间:2018-10-23 15:35:01

标签: java arrays add

我在教科书中找到一个练习来制作2D数组。

Example input/output

我有一个输入循环,表可以成功打印,但是我找不到一种方法来获取每一行和每一列中的值并打印出总计,如练习中所示。

我问过我的教授,他说他不记得如何用字符串数组来做这件事。我希望有一种方法可以将每个数字从字符串转换为整数。我以为创建双精度数组会比String数组容易得多,但目前我不知道将所有工作都转换过来。

package Assignment2;
import java.util.*;

/**
 *
 * @author Lyan
 */
public class Exercise7_20
{
public static void sales2DArray(int salesPerson, int product, double value)
{

}


public static void main(String[] args)
{

        int salesP = 0; //salesPerson set to 0






     String[][] table = new String[5][5]; // A matrix of '5 rows and '5 Columns'


     //For loop that replaces null values in table with 0.00
     for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
    if (table[i][j] == null) {
        table[i][j] = "       0.0";
    }
}
}
     //Input for salesPerson
        Scanner inSales = new Scanner(System.in);

        System.out.println("Enter salesPerson number (-1 to end): ");
        salesP = inSales.nextInt();


        //While loop to ask for salesPerson, product, and sales amount (val)
    while(salesP > 0 && salesP < 5)
    {

        //input for Product number
          Scanner inProduct = new Scanner(System.in);
    System.out.println("Enter product number");
    int productNum = inProduct.nextInt();

    //input for sales amount
    Scanner inValue = new Scanner(System.in);
    System.out.println("Enter Sales amount");
    double val = inValue.nextDouble();

    //sets the values onto the table.
    table[productNum - 1][salesP] = "       " + Double.toString(val);

                System.out.println("Enter salesPerson number (-1 to end): ");

                salesP = inSales.nextInt();

                //makes the 1-5 on the left hand side of the table
    }
    for(int i = 1; i < 6; i++)
    {
         table[i-1][0] = "       " + i + "";
    }

    //Hardcoded header
    System.out.println("Product Salesperson 1 Salesperson 2 Salesperson 3 Salesperson 4 Total");

    //Makes the 2D array to print in a box format rather than a straight line.
System.out.println(Arrays.deepToString(table).replace("],","]\n"));


//Anything below is my testing to get the total to print out for each individual salesPerson (column)
//and the totals for the products for all salesPerson (rows)
    System.out.print("Total       ");

     String total = "";
     int sum = 0;

    for(int down = 0; down < 5; down++)
    {

            //append
    }


    //successfully reads the last value of each column but does not print the total
    //only successfully prints the last value
    for(int c = 1; c < 5; c++)
    {
    for(int r = 0; r < 5; r++)
    {
       String temp = table[r][c];
       total = temp;
    }

    System.out.print(total + "   ");
    }
}
}

1 个答案:

答案 0 :(得分:0)

您可以使用这种简单的逻辑来查找2D数组的每一行和每一列的总和。

double[] sumOfRows = new double[5];
double[] sumOfCols = new double[5];

for (int i = 0; i < table.length; i++) {
    for (int j = 0; j < table.length; j++) {
        sumOfRows[i] = sumOfRows[i] + Double.parseDouble(table[i][j]);
        sumOfCols[i] = sumOfCols[i] + Double.parseDouble(table[j][i]);
    }
}

在这里,我们声明了两个额外的数组,分别存储每个行和列的总和 。您可以根据需要按逻辑打印它们。

还请注意使用Double.parseDouble(...) String 转换为 double