Java program using arrays simple coding for project

亡梦爱人 提交于 2019-12-20 07:56:07

问题


I just want "Jan Expenditure" to appear however only expenditure comes out. How do I do it? Jan is from my monthsArrays. Is there anything missing?

import java.util.Scanner;

public class Project {

static int choice;

public static void main(String[] args) {

    {   Scanner input = new Scanner(System.in);
        System.out.println("***************Expenditure***************");
        System.out.println("1)Enter monthly expenses");
        System.out.println("2)Display detailed expenditure by month");
        System.out.println("3)Quick glance at monthly expenses");
        System.out.println("4)Exit");
        System.out.println("Please select your choice <1-3>:");
        choice = input.nextInt();


        switch (choice) {
        case 1:
            int count = 0;
            String[] monthsArray = { "", "Jan", "Feb", "Mar", "Apr", "May",
                    "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };
            System.out.println("*******************************************");
            System.out.println("\t\t\t\t");
            System.out.print("Enter month <1 for Jan - 12 for Dec>:");
            int month = input.nextInt();
            for (int i=0; i < monthsArray.length; i++)
            String monthChoice = monthsArray[month - 1];
            System.out.println("-------------------------------------");
            System.out.println(monthChoice + "expenditure (max 10 items)");

This is the output i'm getting currently

******Expenditure******

1)Enter monthly expenses

2)Display detailed expenditure by month

3)Quick glance at monthly expenses

4)Exit

Please select your choice <1-3>: 1


Enter month <1 for Jan - 12 for Dec>:1


expenditure (max 10 items)

Enter item 1:

As you can see the array "Jan" is not appearing.


回答1:


From the looks of your monthsArray, the first element is a blank string. Remember that arrays are zero-based, and you asked your user for a month between 1 and 12 (which is fine, because nobody thinks of January as month 0, right?). Later in your code, you do this:

String monthChoice = monthsArray[month - 1];

However, your array actually contains 13 elements, because you added that blank space, so in your case, you're actually safe to simply use the unaltered month index, like so:

String monthChoice = monthsArray[month];

OR, get rid of the empty string at the beginning of the array, and leave your monthChoice line as is (depends on how your implementation is supposed to work. That is purely your choice).

Some side notes:

You are presently declaring your monthArray in the body of the switch statement, which is unnecessary. It would be better to declare it at the top of the method, OR even better, as a constant in the class, like so:

private static final String[] monthsArray = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };

Also, this line:

for (int i=0; i < monthsArray.length; i++)

is useless. I don't know whether this is part of an unfinished solution, however all it is doing is repeating the statement that immediately follows it, which as as far as I can tell, is unnecessary. Basically it is the same as this:

for (int i=0; i < monthsArray.length; i++){
    String monthChoice = monthsArray[month - 1];
}

So you should be safe to get rid of it.




回答2:


You have a typical +/-1 error in your code.

Your monthsArray has an empty String at position 0 and you take the displayed String from month -1. Simply entering 2 and getting "Jan expenditure (max 10 items)" as a result should have been a good hint.



来源:https://stackoverflow.com/questions/21434924/java-program-using-arrays-simple-coding-for-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!