Requirements:
1.How to display the stored data for each individual month without having to copy and paste code several times for the DisplayMonthData() method?
2.Is my code for summing up all the values for each开发者_Go百科 month correct in DisplayMonthData()?
Thanks in advance.
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()
{
if(months[m]=="Jan"){
for(int row=1;row<amount.length;row++){
for(int column=0;column<amount[row].length;column++){
//janItems[]+=monthItems[1][0];
if(amount[row][column]!=0){
System.out.println(monthItems[row][column]+"\t$"+fmt.format(amount[row][column]));
}else{}
}
}
sum[L]+=amount[1][0];
System.out.println();
System.out.println("Total amount spent for the month of January is $"+fmt.format(sum[L]));
L++;
}
}
- Add a parameter to take the month in the DisplayMonthData method. (Also make it start with a lower case letter per Java convention).
- Define correct. (Also use
.equals
for comparison of objects like Strings instead of==
)
精彩评论