如何显示我的代码?

时间:2021-03-11 14:58:57

My question is how do I display the 2D array which I stored.

我的问题是如何显示我存储的2D数组。

  1. Specifically, my program needs to display the elements (in somewhat a table) which I stored in them for each month.
  2. 具体来说,我的程序需要显示我每月存储在其中的元素(在某种程度上是一个表格)。

  3. Next, it should sum up all the values stored(do I use another array for that?), and display them in a line, e.g. (System.out.println("Total amount spent for the month of January is.....")
  4. 接下来,它应该总结存储的所有值(我是否使用另一个数组?),并将它们显示在一行中,例如: (System.out.println(“1月份的总支出是.....”)

All help is appreciated!

所有帮助表示赞赏!

import java.util.*;
import java.text.*;

public class test {
    static Scanner input = new Scanner(System.in).useDelimiter("\r\n");
    static DecimalFormat fmt = new DecimalFormat("0.00");
    static String months[] = { null, "Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; // to store month data
    static String monthItems[][] = new String[13][13];
    static double amount[][] = new double[13][13];
    static int m = 1;

    public static void main(String[] args) {
        try {
            System.out.print("Enter month <1 for Jan - 12 for Dec>:  ");
            m = input.nextInt();
            if (m == 0) {
                System.out.println("----------------------------------------");
                System.out.println("Invalid month!");
                System.out.println();
                return;
            } else {
            }
            System.out.println("----------------------------------------");
            System.out.println(months[m] + " Expenditure <max 10 items>");
            // to store data if January is selected
            MonthData();
        } catch (Exception e) {
            // error message to inform user of wrong input
            System.out.println("Invalid month!");
        }

        try {
            System.out.print("Enter month <1 for Jan - 12 for Dec> :  ");
            m = input.nextInt();
            EntryMonth();
            System.out.println("----------------------------------------");
            System.out.println("Expenditure for " + months[m] + ":");
            // to display monthly data
            DisplayMonthData();
        } catch (Exception e) {
            // when user enter's wrong input
            System.out.println("Invalid month!");
        }
    }

    static void EntryMonth()// to allow user to enter month data
    {
        try {
            if (m < 1 || m > 12) {
                return;
            } else {
            }
        } catch (Exception e) {
            System.out.println();
        }
    }

    static void MonthData() {
        try {
            for (int i = 0; i < 10; i++) {
                System.out.print("Enter item " + (i + 1)
                        + " <Press ENTER to exit> : ");
                monthItems[m][i] = input.next();
                if (monthItems[m][i].length() == 0) {
                    return;
                } else {
                    System.out.print("Enter amount : $");
                    amount[m][i] = input.nextDouble();
                    System.out.println("");
                }
            }
        } catch (Exception e) {
            System.out.println("");
        }
    }

    static void DisplayMonthData() {
        for (int row = 0; row < amount.length; row++) {
            for (int column = 0; column < amount[row].length; column++) {
                System.out.println(monthItems[row][column]);
            }
        }
    }
}

2 个解决方案

#1


0  

I'm not allowed to comment Joeri Hendrickx' answer, but he is correct. I just tried your code, entered 10 (enter) foo (enter) 5 (enter) bar (enter) 7 (enter) (enter) 10 (enter) and it displayed everything just fine. Of course all but two items are null and all but two amounts are 0.0, but that is just as expected.

我不允许评论Joeri Hendrickx的回答,但他是对的。我刚刚尝试了你的代码,输入10(输入)foo(输入)5(输入)栏(输入)7(输入)(输入)10(输入)并显示一切正常。当然除了两个项目之外的所有项都是null,除了两个数量之外的所有项都是0.0,但这正如预期的那样。

#2


0  

There are a lot of ways, but here's something which should already help you.

有很多方法,但这里应该有一些帮助你。

  static void DisplayMonthData()
  {
    for(int row=0;row<amount.length;row++){
        for(int column=0;column<amount[row].length;column++){
            String monthItem = monthItems[row][column];
            if (monthItem==null)
                System.out.print("\t")
            else
                System.out.print(monthItems[row][column]+"\t");
        }
        System.out.print("\n");
    }
  }

If you want the totals next to each line, refactor out the part that prints one line, and make it sum the values in that line. Then add the end, add a print(total).

如果您希望每行旁边的总计,请重构打印一行的部分,并使其与该行中的值相加。然后添加结束,添加打印(总计)。

Edited to skip null values.

编辑跳过空值。

#1


0  

I'm not allowed to comment Joeri Hendrickx' answer, but he is correct. I just tried your code, entered 10 (enter) foo (enter) 5 (enter) bar (enter) 7 (enter) (enter) 10 (enter) and it displayed everything just fine. Of course all but two items are null and all but two amounts are 0.0, but that is just as expected.

我不允许评论Joeri Hendrickx的回答,但他是对的。我刚刚尝试了你的代码,输入10(输入)foo(输入)5(输入)栏(输入)7(输入)(输入)10(输入)并显示一切正常。当然除了两个项目之外的所有项都是null,除了两个数量之外的所有项都是0.0,但这正如预期的那样。

#2


0  

There are a lot of ways, but here's something which should already help you.

有很多方法,但这里应该有一些帮助你。

  static void DisplayMonthData()
  {
    for(int row=0;row<amount.length;row++){
        for(int column=0;column<amount[row].length;column++){
            String monthItem = monthItems[row][column];
            if (monthItem==null)
                System.out.print("\t")
            else
                System.out.print(monthItems[row][column]+"\t");
        }
        System.out.print("\n");
    }
  }

If you want the totals next to each line, refactor out the part that prints one line, and make it sum the values in that line. Then add the end, add a print(total).

如果您希望每行旁边的总计,请重构打印一行的部分,并使其与该行中的值相加。然后添加结束,添加打印(总计)。

Edited to skip null values.

编辑跳过空值。