大家?guī)臀铱纯催@段代碼有什么問題,編譯不了?。。。?/h1>

package com.jun;
public class Test {
public static void main(String[] args){
TryCatchTest tct = new TryCatchTest();
int result=tct.test();
}
public class TryCatchTest{
int divider = 10;
int result = 100;
public int test(){
try{
while(result>-1)
result = result+100/divider;
return result;
}catch(Exception e){
e.printStackTrace();
System.out.println("拋出異常了");
return -1;
}
}
}
}
package com.jun;
public class Test {
public static void main(String[] args){
TryCatchTest tct = new TryCatchTest();
int result=tct.test();
}
public class TryCatchTest{
int divider = 10;
int result = 100;
public int test(){
try{
while(result>-1)
result = result+100/divider;
return result;
}catch(Exception e){
e.printStackTrace();
System.out.println("拋出異常了");
return -1;
}
}
}
}
2016-03-30
因為TryCatchTest是一個動態(tài)的內部類,創(chuàng)建這樣的對象必須有實例與之對應,程序是在靜態(tài)方法中直接調用動態(tài)內部類會報這樣錯誤。這樣的錯誤好比類中的靜態(tài)方法不能直接調用動態(tài)方法。可以把該內部類聲明為static。或者不要在靜態(tài)方法中調用。
public static class TryCatchTest
{
}
2016-03-30
你沒有輸出結果的語句啊,定義了整形變量 result 接受函數的輸出結果,但是這個結果你沒有處理啊? 應該在int result=tct.test(); 后加上輸出語句 System.out.println(result);
2016-03-30
我看錯了。。。。
2016-03-30
因為一個java文件中只能有一個public class,
你把第二個類放在其他java文件中,或者是將第二個類的public修飾符去掉,就可以了。
2016-03-30
/**
?* 錯誤在于TryCatchTest類不要放到主程序中....,這個運行正確
?* */
public class TryCatchTest {
int divider = 10;
int result = 100;
public int test(){
try{
while(result>-1)
result = result+100/divider;
return result;
}catch(Exception e){
e.printStackTrace();
System.out.println("拋出異常了");
return -1;
}
}
public static void main(String[] args){
TryCatchTest tct = new TryCatchTest();
int result=tct.test();
}