快速問題。有什麼辦法可以做到這一點嗎?嘗試捕獲數組初始化
int array [] = new int [
(try {
Integer.parseInt (/*get string input here*/);
}
catch (NumberFormatException e){
//error handling here
)
];
快速問題。有什麼辦法可以做到這一點嗎?嘗試捕獲數組初始化
int array [] = new int [
(try {
Integer.parseInt (/*get string input here*/);
}
catch (NumberFormatException e){
//error handling here
)
];
這不是合法的語法。嘗試這樣的:
int number;
try
{
number = Integer.parseInt (/*get string input here*/);
}
catch (NumberFormatException e)
{
//error handling here
}
int array [] = new int [2];
array[0] = number;
這是一個有點冗長,但歡迎來到Java,我猜。
像Brian說的那樣..你需要將你的try/catch塊移到數組初始化的上方。
int i;
try{
string myString = "1";
i = Integer.parseInt(myString)l
} catch(NumberFormatException e){
//handle
}
int array [] = new int[3];
見下文......
int myArray[] = new int[10];
String tempString = "1";
try {
for (int i=0;i<=9;i++) {
tempString += i;
myArray[i]=Integer.parseInt(tempString);
}
} catch (NumberFormatException nfe) {
// print error
} catch (Exception e) {
// print error
}
是。在*初始化數組之前執行*。 – 2012-02-18 05:20:59