亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

大家幫我看看這段代碼有什么問題,編譯不了?。。?!

http://img1.sycdn.imooc.com//56fbadec0001406b11380448.jpg

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;

}

}

}

}


正在回答

5 回答

因為TryCatchTest是一個動態的內部類,創建這樣的對象必須有實例與之對應,程序是在靜態方法中直接調用動態內部類會報這樣錯誤。這樣的錯誤好比類中的靜態方法不能直接調用動態方法??梢园言搩炔款惵暶鳛閟tatic?;蛘卟灰陟o態方法中調用。

public static class TryCatchTest
{


}


0 回復 有任何疑惑可以回復我~

你沒有輸出結果的語句啊,定義了整形變量 result 接受函數的輸出結果,但是這個結果你沒有處理啊? 應該在int result=tct.test(); 后加上輸出語句 System.out.println(result);

1 回復 有任何疑惑可以回復我~

我看錯了。。。。

0 回復 有任何疑惑可以回復我~

因為一個java文件中只能有一個public class,

你把第二個類放在其他java文件中,或者是將第二個類的public修飾符去掉,就可以了。

0 回復 有任何疑惑可以回復我~

/**

?* 錯誤在于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();

}


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

大家幫我看看這段代碼有什么問題,編譯不了!?。?!

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號