KRNNNS 2022-07-28 13:01 采纳率: 100%
浏览 386
已结题

java入门 找不到局部变量

问题遇到的现象和发生背景

img

问题相关代码,请勿粘贴截图

package service;
import domain.;
import domain.Equipment;
import static service.Data.EQUIPMENTS;
import static service.Data.
;

/**

  • @description: 负责将Data中的数据封装到Employee[]数组中,同时提供相关操作Employee[]的方法

  • /
    public class NameListService {
    private Employee[] employees;

    public NameListService(){

              employees=new Employee[Data.EMPLOYEES.length];
    
              for (int i = 0;i<employees.length;i++){
                  //Architect :  13, id, name, age, salary, bonus, stock
                  int type=Integer.parseInt(Data.EMPLOYEES[i][0]);
                  int id=Integer.parseInt(EMPLOYEES[i][1]);
                  String name=EMPLOYEES[i][2];
                  int age=Integer.parseInt(Data.EMPLOYEES[i][3]);
                  int salary=Integer.parseInt(Data.EMPLOYEES[i][4]);
                  Equipment eq;
                  double bouns;
                  int stock;
                  switch (type){
                      case EMPLOYEE:
                          employees[i]=new Employee( id, name, age, salary);
    
                      case PROGRAMMER:
                          eq=createquipment(i);
                          employees[i]=new Programmer(id, name, age, salary,eq);
    
                      case DESIGNER:
                          eq=createquipment(i);
                           bouns=Double.parseDouble(EQUIPMENTS[i][5]);
                          employees[i]=new Designer( id, name, age, salary,eq,bouns);
    
                      case ARCHITECT:
                          eq=createquipment(i);
                          bouns=Double.parseDouble(EQUIPMENTS[i][5]);
                          stock=Integer.parseInt(EQUIPMENTS[i][6]);
                          employees[i]=new Architect( id, name, age, salary,eq,bouns,stock);
                  }
              }
    

    }

private Equipment createquipment(int index){
    int type=Integer.parseInt(EQUIPMENTS[index][0]);
    switch (type){
        //        {"22", "联想T4", "6000"},
        //        {"21", "戴尔", "NEC17寸"},
        //        {"21", "戴尔", "三星 17寸"},
        //        {"23", "佳能 2900", "激光"},
        case PC:
            return new PC(EQUIPMENTS[index][1], EQUIPMENTS[index][2]);
        case NOTEBOOK:
            int price=Integer.parseInt(EQUIPMENTS[index][2]);
            return new NoteBook(EQUIPMENTS[index][1], price);
        case PRINTER:
            return new Printer(EQUIPMENTS[index][1],EQUIPMENTS[index][2]);
    }
    return null;
}
//获取所有员工
public Employee[]  getAllEmployees(){
    return employees;
}
public Employee  getEmployees (int id)throws TeamException{
    for (int i=0;i<employees.length;i++){
        if(employees[i].getId()==id)
            return employees[i];
    }
    throw new TeamException("不存在此员工");
}

}

运行结果及报错内容

img

  • 写回答

3条回答 默认 最新

  • 林晓风 2022-07-28 13:33
    关注

    看报错,鼠标点到那个蓝色的字上面,56行,报错说数组越界了,检查一下你的数组

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月5日
  • 已采纳回答 7月28日
  • 修改了问题 7月28日
  • 创建了问题 7月28日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵