首先,我跳过了员工姓名部分,因此我添加了k.nextLine以消耗\ n之前。 现在我有这样的:为什么在输入最后一个int数组时输入java.lang.ArrayIndexOutOfBoundsExeption?
折扣SUPERMALL
[销售报告]
输入员工数:3
员工姓名:一个
员工销售:1
员工姓名:S
员工销售:2
员工姓名:d
员工销售:3
继承人我所做的:
public static void main(String[] args)
{ Scanner k = new Scanner(System.in);
System.out.println("DISCOUNT SUPERMALL");
System.out.println(" [Sales Report]");
System.out.print("Enter number of Employee: ");
int numberOfEmp = k.nextInt();
String[] empName = new String [numberOfEmp];
double[] empSale = new double [numberOfEmp];
for(int a=0; a<numberOfEmp; a++)
{ System.out.print("Employee Name: ");
k.nextLine();
empName[a] = k.nextLine();
System.out.print("Employee's Sales: ");
empSale[a] = k.nextDouble();
} System.out.println();
for (int c=0; c<numberOfEmp; c++)
{ System.out.println("Name: "+empName[numberOfEmp]);
System.out.println("Sales: "+empSale[numberOfEmp]);
}
}
如果数组大小的'3',您可以使用最后一个可能的索引是'2'('3-1'),因为索引是从0开始 – Shark
应该只使用这个:'系统.out.println(“Name:”+ empName [(numberOfEmp> = 1)? (numberOfEmp-1):0]);' – Shark