有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在字符串类型的2D数组中添加列和行?

我在教科书中找到了一个制作这个2D数组的练习

Example input/output

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

我问过我的教授,他说他不记得如何使用字符串数组。我希望有一种方法可以将每个数字从string转换为int。我假设创建双数组要比创建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) 个答案

  1. # 1 楼答案

    您可以使用这个简单的逻辑来查找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(...)字符串转换为