我製作棋盤,只有國王纔會按照國際象棋的規則走棋。 在它的方法下面,它爲King和i的j座標調用了2次,我使用輸入變量String來檢查這個King的座標是否已經存在。比我嘗試將其轉換爲整數,似乎這個轉換有問題。字符串到整數轉換有點問題
import java.util.*;
public class King {
int move(String iK){
Random rand = new Random();
Integer coordinateKing = Integer.valueOf(iK);
if (iK == null){
coordinateKing = rand.nextInt(8);
}else{
int caseI;
switch(caseI = rand.nextInt(2)){
case 0: if (coordinateKing < 8){ coordinateKing++; } else {caseI = rand.nextInt(2);}
break;
case 1: if (coordinateKing > 0){ coordinateKing--; } else {caseI = rand.nextInt(2);}
break;
default:
break;
}
}
return coordinateKing;
}
}
我的問題是這樣的:
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.valueOf(Integer.java:582)
at chess_engine.King.move(King.java:6)
at chess_engine.MainClass.main(MainClass.java:12)
提前感謝!
你能提供'iK'的值嗎? –
'iK'是'null',因此您無法解析它。你可能想要聲明'coordinateKing'並在else塊中移動 'coordinateKing = Integer.valueOf(iK);' –