使用数组项目编码简单的Java程序 [英] Java program using arrays simple coding for project

查看:235
本文介绍了使用数组项目编码简单的Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想简支出却出现仅支出出来。我该怎么做?一月是从我monthsArrays。有没有遗漏什么吗?

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)");

这是我得到目前输出

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

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

1)进入每月的费用

2)显示按月详细的开支

2)Display detailed expenditure by month

3)每月的开支看一眼

3)Quick glance at monthly expenses

4)退出

请选择您所选择&LT; 1-3> 1

Please select your choice <1-3>: 1

输入一个月&LT; 1月 - 为12月12日> 1

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

支出(最多10个项目)

expenditure (max 10 items)

输入项目1:

正如你所看到的阵列扬没有出现。

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

推荐答案

从你的 monthsArray ,第一个元素是一个空字符串的样子。请记住,数组是从零开始,你问你的用户一个月1到12之间(这是很好的,因为没有人认为月为月0,对吧?)。后来在你的code,你这样做:

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];

不过,您的数组实际上包含13个元素,因为你还说空白,所以你的情况,你其实可以安全简单地使用不变指数,像这样:

String monthChoice = monthsArray[month];

,或者在数组的开始摆脱空字符串,并留下您的 monthChoice 行是(取决于你的实现是如何工作的,这纯粹是你的选择)。

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).

一些旁注:

您是presently声明你的 monthArray 在switch语句中,这是不必要的身体。这将是最好的方法,甚至更好的顶部声明它,如类中的常数,如:

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" };

此外,该行:

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.

这篇关于使用数组项目编码简单的Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆