简体   繁体   English

线程“主”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0

[英]Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0

I'm new at learning java, and while following a tutorial from a book, I received this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0. I've tried researching from the net to find out more about the error, and I could not find the answer to this.我是学习 java 的新手,在按照书中的教程进行操作时,我在线程“main”java.lang.ArrayIndexOutOfBoundsException: 0 中收到此错误异常:0。我尝试从网上进行研究以了解有关该错误的更多信息,我找不到这个问题的答案。 To make things worse, the book's website no longer exists when I tried to go to their website.更糟糕的是,当我尝试将 go 转到他们的网站时,这本书的网站已不存在。

The program is about calculating payments for 2 types of workers, Engineers and Technicians including Over time pays which is 1.5x the original salaries for the 2 different workers.该计划是关于计算 2 类工人、工程师和技术人员的工资,包括加班工资,这是 2 位不同工人原始工资的 1.5 倍。 Max working hours is 160 hours, and additional hours trigger the overtime rate.最长工作时间为 160 小时,额外工作时间触发加班率。

Here's the code that I wrote:这是我写的代码:

class PayCalculator3 {
    public static void main (String []args) {
    final int maxNoOverTime = 160;
    final double engineerHourlyPay = 30;
    final double technicianHourlyPay = 25.5;
    final double overTimeRate = 1.5; 
    int position = Integer.parseInt(args[0]);
    int hoursWorked = Integer.parseInt(args[1]);
    double salary;

salary = 
(position == 0) ? 
// employee is an Engineer
(hoursWorked <= maxNoOverTime) ?
    // he did not work overtime
    (hoursWorked * engineerHourlyPay)
    :
    // he worked overtime
    ((maxNoOverTime * engineerHourlyPay) + ((hoursWorked - maxNoOverTime) * (engineerHourlyPay * overTimeRate))) 
: (position == 1) ?
// if employee is a Technician
(hoursWorked <= maxNoOverTime) ?
    // he did not work Overtime
    (hoursWorked * technicianHourlyPay)
    :
    // he worked overtime
    ((maxNoOverTime * technicianHourlyPay) + ((hoursWorked - maxNoOverTime) * (technicianHourlyPay * overTimeRate)))

:
//Employee Type unknown
-1;
String str = (salary != -1) ?
        ("This month's salary >> $" + salary)
        :
        ("Who the heck are you?");
System.out.println(str);
    }
}

Thank you very much in advance for all of your kind help:)非常感谢您提前提供的所有帮助:)

You must not be passing two arguments to the program.您不得将两个 arguments 传递给程序。 Where you are doing args[0] it's expecting an integer passed via the command line to the program.在您执行 args[0] 的地方,它期望 integer 通过命令行传递给程序。 Please add how you are invoking the program.请添加您调用程序的方式。

java PayCalculator3 10 10

You should be invoking it with two integers as the argument, as per the example above.根据上面的示例,您应该使用两个整数作为参数来调用它。

so basically you have to go in and call this as such in your cmd or what ever you are using else you will recieve the errors ie you aren't putting in parameters.所以基本上你必须输入 go 并在你的 cmd 中调用它,否则你会收到错误,即你没有输入参数。在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 7 - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:6 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 6 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:2 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:5 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException: - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:-1 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException 4 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 4 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:3 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:8 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM