【问题标题】:Adding columns and rows in a 2D array of String type?在字符串类型的二维数组中添加列和行?
【发布时间】:2018-10-23 15:35:00
【问题描述】:

我在我的教科书中找到了一个制作这个二维数组的练习。

我的输入循环正常工作,表格打印成功,但我找不到一种方法来获取每一行和每一列中的值并打印出练习中所示的总数。

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

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 + "   ");
    }
}
}

【问题讨论】:

  • 参见Double.parseDouble() 将字符串转换为双精度。但是为什么要使用String[][]?为什么不使用double[][]int[][]

标签: java arrays add


【解决方案1】:

您可以使用这个简单的逻辑来计算二维数组的每一行和每一列的总和。

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

这里我们声明了 2 个额外的数组来分别存储每一行​​和每一列的总和。您可以根据需要根据自己的逻辑打印它们。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    相关资源
    最近更新 更多