Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我只输入2个输入时,为什么else语句不起作用?_Java_String_Accounting - Fatal编程技术网

Java 当我只输入2个输入时,为什么else语句不起作用?

Java 当我只输入2个输入时,为什么else语句不起作用?,java,string,accounting,Java,String,Accounting,好的,我正在开发我的会计应用程序,以下是我目前得到的信息: public class Accounting { public static void main(String[] args) { while(true){ Scanner input = new Scanner(System.in); String userinput = input.nextLine(); String[] parts

好的,我正在开发我的会计应用程序,以下是我目前得到的信息:

public class Accounting {
    public static void main(String[] args) {
        while(true){
            Scanner input = new Scanner(System.in);
            String userinput = input.nextLine();

            String[] parts = userinput.split(" ");
            String part1 = parts[0];
            String part2 = parts[1];
            String part3 = parts[2];

            int a = Integer.parseInt(part1);
            float r = Float.parseFloat(part2);
            int t = Integer.parseInt(part3);
            double Total = a*Math.pow(1.0+(r/100.0), t);

            String[] result = userinput.split(" ");
            if (result.length == 3) { 
                System.out.println(Total);
            } else {
                System.out.println("Usage: a r t (a is the amount, r is the rate, and t is the time)");
            }
        }
    }
}   
我这样做是为了,如果用户输入超过3个输入,它将给出使用提示。然而,当我输入2个输入时,我会得到一个错误而不是使用提示,即使2不等于3

下面是错误消息:

主线程java.lang.ArrayIndexOutOfBoundsException中的异常:2 在Accounting.maincounting.java:15

我怎样才能解决这个问题


编辑:我这里的问题不是因为只有两个输入,所以数组部分不存在,而是它不会给出使用提示。

原因是您试图访问不存在的数组部分:

字符串part3=零件[2]

其中parts.length==2


我假设您得到一个索引越界错误?

当您只有2个输入时,部件数组的范围是[0..1],因此当您尝试在此处访问部件[2]时:

   String part3 = parts[2];
将抛出一个错误。

我这里的问题不是因为只有两个输入,所以数组的部分不在那里,而是它不会给出使用提示

不,你错了。关键是,在使用假定长度的数组之前,需要检查数组的长度。这是一个简单的重新排列,应该可以正常工作,此外,您可能缺少一些东西,比如退出条件,并试图从解析中获得NumberFormatException:

Scanner input = new Scanner(System.in);

while(true){
    String userinput = input.nextLine();

    String[] parts = userinput.split(" ");

    // check first
    if (parts.length != 3){
        System.out.println(
            "Usage: a r t (a is the amount, r is the rate, and t is the time)"
        );
        continue; // skip the computation if the input is wrong
    }

    String part1 = parts[0]; // these lines WILL throw an error
    String part2 = parts[1]; // if you attempt to access elements
    String part3 = parts[2]; // of the array that do not exist

    int a = Integer.parseInt(part1);
    float r = Float.parseFloat(part2);
    int t = Integer.parseInt(part3);
    double total = a*Math.pow(1.0+(r/100.0), t);

    System.out.println(total);
}

我假设到目前为止答案是正确的,但在将来,如果您包含错误消息,这将有所帮助。