簡體   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