繁体   English   中英

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

[英]Adding columns and rows in a 2D array of String type?

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

输入/输出示例

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

我问过我的教授,他说他不记得如何用字符串数组来做这件事。 我希望有一种方法可以将每个数字从字符串转换为整数。 我以为创建一个双精度数组会比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 + "   ");
    }
}
}

您可以使用这种简单的逻辑来查找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]);
    }
}

在这里,我们声明2个的附加阵列,以分别存储每行和列的总和。 您可以根据需要根据逻辑打印它们。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM