2013-02-24 49 views
0

所以我試圖構建一個程序。一切工作,但我的程序的數學部分。我希望輸入的分鐘時間轉換爲數小時。這應該是一個十進制數字,例如30分鐘= 0.5小時。我會盡量忽略儘可能多的代碼,以致於我認爲這些代碼不會使其更易於閱讀。爲什麼我的變量不能返回答案?

System.out.println("What is the total amount of time spent reading?: "); 
totalTime=scanner.nextInt(); 
System.out.println(StudentReader.getTotalTime()); 
+3

有在你的類中沒有'getTotalTime()'方法。你只有一個'getTotalTimeInHours'。 – Leeish 2013-02-24 19:50:34

+0

另外,您並未使用可能讀取分鐘數的totalTime字段。 – 2013-02-24 19:52:28

+0

@Leeish哇我不敢相信我看過那個。 Thx – SkyVar 2013-02-24 19:54:30

回答

4

這是一個問題::

public class StudentReader 
{ 
    private static String studentName= ""; 
    private static int pages; 
    private static int time; 
StudentReader(String name,int pagenum, int totalTime) 
    { 
     studentName=name; 
     pages=pagenum; 
     time=totalTime; 
    } 
public double getTotalTimeInHours() 
    { 
     double total=0; 
     total=time/60; 
     return total; 

    } 
} 

該類正在被另一個類稱爲

total=time/60; 

兩個time60int值,因此它使用整數除法,並然後將結果提升爲double值,以便將其分配給total

這個簡單的變化將迫使它使用double算術:

total=time/60.0; 

然而,鑑於你實際上並沒有做任何其他與total除歸還,代碼會更簡單爲:

public double getTotalTimeInHours() { 
    return time/60.0; 
} 

此外,作爲Leeish指出的那樣,你想稱其爲getTotalTime,這不是方法名稱...

相關問題