java - 为什么我的代码中不断出现 arrayIndexOutOfBound 异常错误
问题描述
我的代码用于实现多项式并将其转换为对应的导数,例如 3x^2 + 2x 导数是 6x + 2 但由于某种原因,每次我在 main 方法中运行我的代码时,它都会说我有时会得到一个 arrayIndexOutOfBoundException即使类型不匹配,我的代码逻辑是否错误?
公共课德{
private static Der[] polyVal;
private String ope;
private int coe;
private String var;
private int exp;
private String ope2;
private static int deg;
Der(String ope, int coe, String var, int exp, String ope2) {
this.ope = ope;
this.coe = coe;
this.var = var;
this.exp = exp;
this.ope2 = ope2;
}
//Accessor methods
public String getOpe() {return this.ope;}
public int getCoe() {return coe;}
public String getVar() {return var;}
public int getExp() {return exp;}
public String getOpe2() {return this.ope2;}
// mutator methods
public void setOpe(String newOpe) {this.ope = newOpe;}
public void setCoe(int newCoe) {this.coe = newCoe;}
public void setVar(String newVar) {this.var = newVar;}
public void setExp(int newExp) {this.exp = newExp;}
public void setOpe2(String newOpe2) {this.ope2 = newOpe2;}
public static void setEle(int degree) {
deg = degree;
polyVal = new Der[deg+1];
}
/*
public void setValues(Der p) {
int i = 0;
for(i; i < deg; i++) {
}
}
*/
public void addObj(Der newDer) {
int size = polyVal.length-1;
int k = 0;
while(polyVal[k] == null && k < size) {
polyVal[k] = newDer;
k++;
}
}
public static Der[] differentiate(Der[] polynomial) throws IllegalArgumentException {
Der[] diffPoly = polynomial.clone();
for(int i = 0; i < deg+1; i++) {
Der newP = diffPoly[i];
int dCoe = newP.getCoe();
if(dCoe == 0) {dCoe = 1;}
String dVar = newP.getVar();
if(dVar.indexOf(1) != '^') {throw new IllegalArgumentException("Invalid format");}
int dExp = newP.getExp();
if(dExp != 0 || dExp != 1) {
dCoe = dCoe * dExp;
dExp = dExp-1;
newP.setCoe(dCoe);
newP.setExp(dExp);
//newP.setVar(this.var);
//newP.setOpe(this.ope);
//newP.setOpe2(this.ope2);
}else if(dExp == 1) {
dExp = 0;
newP.setExp(dExp);
//newP.setVar(this.var);
//newP.setOpe(this.ope);
//newP.setOpe2(this.ope2);
}else if(dExp == 0 || dVar == "") {
dCoe = 0;
newP.setCoe(dCoe);
//newP.setVar(this.var);
//newP.setOpe(this.ope);
//newP.setOpe2(this.ope2);
}
diffPoly[i] = newP;
if(diffPoly[0].getOpe() == "+") {throw new IllegalArgumentException("Invalid format");}
}
return diffPoly;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("pleas enter the degree of the polynomial");
int degreeVal = sc.nextInt();
//setEle(degreeVal);
System.out.println("Now start adding in the expression in the format" + "- or noting" + "some int" + "some letter with ^" + "some int" + "+ or -");
Der[] polynomial = new Der[degreeVal];
int j = 0;
while(j < degreeVal+1){
Der obj = new Der(sc.next(), sc.nextInt(), sc.next(), sc.nextInt(), sc.next());
polynomial[j] = obj;
j++;
}
differentiate(polynomial);
for(int u = 0; u < degreeVal; u++) {
System.out.println(polynomial[u]);
}
}
}
解决方案
推荐阅读
- pandas - Pandas 复杂排序
- java - 如何在 Spring Framework 的前端接收验证消息?
- html - 边距自动未居中
- reactjs - ChunkLoadError:加载块 XY 失败。- 在生产中随机致命
- javascript - 如何避免在Android中重新渲染相机?
- php - 将 Ymd H:i:s 日期时间转换为月名、日数、年数、时间(上午或下午)
- javascript - 启动 nanoexpress 服务器失败
- react-native - React Native navigation TypeError: undefined is not an object (evalating 'Object.keys(routeConfigs)')
- matplotlib - Matplotlib imread() 颜色设置参数建议
- python - 使用 Pandas 获取数据集中的前一行值