2013-10-19 55 views
0
import java.util.Scanner; 

class Operation { 
double add(double a, double b){ 
    double c; 
    c = a+b; 
    return c; 
} 
double sub(double a, double b){ 
    double c; 
    c = a-b; 
    return c; 
} 
double mul(double a, double b){ 
    double c; 
    c = a*b; 
    return c; 
} 
double div(double a, double b){ 
    double c; 
    c = a/b; 
    return c; 
} 
} 

class Selection{  
static double x,y; 
void func(int a){ 
    Scanner sc = new Scanner(System.in); 
    char b; 
    if(a==1) 
     b='+'; 
    else if(a==2) 
     b='-'; 
    else if(a==3) 
     b='*'; 
    else 
     b='/'; 
    System.out.println(">>You have selected "+b+" operator"); 
    System.out.println(">>Please enter the first operand."); 
    x = sc.nextDouble(); 
    System.out.println(">>Please enter the second operand."); 
    y = sc.nextDouble(); 
sc.close(); //line 44, this statement gave me a problem. 
    } 
} 


public class Calculator { 
static int select; 

@SuppressWarnings("static-access") 
public static void main(String [] args){ 
    Operation op = new Operation(); 
    Selection sel = new Selection(); 
    Scanner sc = new Scanner(System.in); 
    boolean run = true; 


    while(run){ 
     System.out.printf(">>Select Operator\n>>1: + 2: - 3: * 4: /\n"); 
     select = sc.nextInt(); 
     double a = sel.x; 
     double b = sel.y; 
     double result; 

     switch(select){ 
     case 1: 
      sel.func(1); 
      a = sel.x; 
      b = sel.y; 
      result = op.add(a, b); 
      System.out.println(">>The result of "+a+" + "+b+" is "+result); 
      break;    
     case 2: 
      sel.func(2); 
      a = sel.x; 
      b = sel.y; 
      result = op.sub(a,b); 
      System.out.println(">>The result of "+a+" - "+b+" is "+result); 
      break;    
     case 3: 
      sel.func(3); 
      a = sel.x; 
      b = sel.y; 
      result = op.mul(a,b); 
      System.out.println(">>The result of "+a+" * "+b+" is "+result); 
      break;  
     case 4: 
      sel.func(4); 
      a = sel.x; 
      b = sel.y; 
      result = op.div(a,b); 
      System.out.println(">>The result of "+a+"/"+b+" is "+result); 
      break; 
     default: 
      System.out.println(">>Your number is not available, please try again!"); 
      System.out.println(); 
      System.out.println(); 
      continue; 
     }  

     System.out.println(">>Do you want to exit the program(y)?"); 
     String startOver = sc.next(); 


     if(startOver.equals("y")){ 
      run = false; 
      System.out.println(">>Thank you for using my program!"); 
     }else{ 
      System.out.println(); 
      continue; 
     } 
sc.close(); //line 111, this works fine i think. 
    } 
} 

} 

我是Java編程的初學者,這是一個簡單的「計算器」代碼,用於我的作業。我希望我的代碼簡單而有效,並且非常努力。檢查代碼後,彈出警告消息,提示「資源泄漏:'sc'永遠不會關閉」。我知道它在沒有添加「sc.close();」的情況下仍然可以正常運行,但我希望我的程序很完美,並添加了「sc.close();」語句添加到第44行和第111行。添加完語句之後,關於資源泄漏的警告消失了,但是當我運行代碼時,程序要求另一次計算的時候,右側會彈出一個調試控制檯。資源泄漏問題

我不知道爲什麼調試控制檯會彈出,你認爲問題是什麼?

回答

0

您應該看到'潛在的資源泄漏...'警告,如果不是,請打開一個錯誤。 這裏的主要問題是編譯器不知道'close(..)'方法的作用。它可能會或可能不會關閉資源。 (請注意,編譯器不執行程序間分析) 您可以選擇忽略「潛在的資源泄漏」警告。 ('資源泄漏'警告保證是正確的,但是'潛在...'不是) 關於資源泄漏分析的更多細節可以在這裏找到 - http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-avoiding_resource_leaks.htm&cp=1_3_9_1

編輯:關於'資源泄漏' vs'潛在的資源泄漏'

這個想法是,所有報告的「資源泄漏」警告都保證是正確的,但可能不是代碼庫中的所有資源泄漏。 '潛在的資源泄漏'很好......潛在的問題。一些開發者在知道出現問題時會打開「潛在...」警告,但他們不知道在哪裏。在這種情況下,潛在的警告有助於縮小搜索範圍。其他一些開發人員會不時瀏覽潛在的警告,看看是否存在真正的問題。 理想情況下,我們希望編譯器能夠給我們提供完整而正確的一組問題,但在實現時存在侷限性:-)

+0

謝謝!幫了我很多! – Muon