我有一个使用以下代码的函数,如下所示:
public static Object generationMode(){
Object[] possibleMode = { "M-Sequence", "Gold Code"};
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleMode, possibleMode[0]);
return selectedValue;
}
我在主类中调用这个函数,它工作正常。但是,我想将“int”返回给主类,而不是“Object”。我尝试将返回值转换如下:
public static int generationMode(){
Object[] possibleMode = { "M-Sequence", "Gold Code"};
int selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleMode, possibleMode[0]);
return (int) selectedValue; // it breaks here
}
但它不工作。调试器在断点处说:(暂停(异常ClassCastException))!!!!那么究竟是什么错误呢?