我的问题是如何显示我存储的二维数组。如何显示我的代码?
- 具体来说,我的程序需要显示每个月我存储在其中的元素(在某种程度上)。
- 接下来,它应该总结所有存储的值(我是否使用另一个数组?),并将它们显示在一行中。 (
System.out.println("Total amount spent for the month of January is....."
)
所有帮助表示感谢!
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]);
}
}
}
}
使用JTable进行显示。如果你能想出一个更具体的问题,问问题。 – 2011-02-15 12:25:26
@Andrew Thompson假设所选的月份是January。用户输入项目字段和金额字段的值。接下来,用户必须能够在该月的价值旁边显示该项目。 – PuppetMinds 2011-02-15 12:32:59