2014-01-15 107 views
-2

這個代碼並且不能進行調試工作,有什麼它應該做的是寫在評論任何想法。感謝所有快速回答的人。任何幫助都會很棒。需要幫助調試,新的節目

/* 
     Debugging: Speed 
     When corrected, this program accepts an int value for starting mileage, an int value for ending mileage, a double value for time of travel, and then performs the calcualtion necessary to output the miles per hour. The program should exit (quit) after the last window is closed. 
    */ 

    public class Speed 
    { 
     public static void main(String[] args) 
     { 
      //Declare Class Variables 
      String highway; 
      int startMiles=0; 
      double endMiles=0; 
      double time= 0; 
      boolean done = false; 

      while (done) 
      { 
       try 
       { 
        String start = JOptionPane.showInputDialog(null,"Enter starting mileage: "); 
        startMiles = Integer.parseInt(start); 

        String end =JOptionPane.showInputDialog(null,"Enter ending mileage: "); 
        endMiles = Integer.parseInt(end); 

        if (endMiles<=startMiles) throw new Exception(); 

        String amount = JOptionPane.showInputDialog(null,"Enter total time as a decimal value: "); 
        gallons = Integer.parseInt(amount); 
        done = true; 
       } 
       catch(Exception e) 
       { 
        JOptionPane.showMessageDialog(null, "Your ending mileage,",endMiles +", must be greater than your starting mileage, " + startMiles, "Error in Mileage", JOptionPane.ERROR_MESSAGE); 
       } 
      } 
      JOptionPane.showMessageDialog(null,"Average speed is "+ (endMiles - startMiles)/time +"miles per hour."); 
     } 
    } 
+4

您是否嘗試過使用調試器? – OnlyThenDidIReckonMyCurse

+0

你在用什麼IDE?蝕?的IntelliJ? NetBeans的? – Mike

+1

仔細查看所有變量及其聲明。 – vandale

回答

2

這是一個問題。

boolean done = false; 

    while (done){ 
     -- As Done is false nothing in here will ever run. 
    } 
4

第一步是寫了一大堆

System.out.println("1"); 

來縮小問題發生。打印出一些重要變量的價值也很有幫助。

在其他的答案提到,這樣的:

boolean done = false; 

System.out.println("1"); 

while(done) { 
    System.out.println("2"); 
    ... 
} 

System.out.println("3"); 

輸出這樣的:

1 
3 

這應該是一個相當大的線索。

+1

舊學校調試仍然非常有用... – Caleryn

+2

我可能不應該在stackoverflow上說這個,但儘管做了一些適度複雜的東西多年來,我從來沒有使用過調試器。 – aliteralmind

+0

即使一些簡單的'Sys.out'語句可以滿足要求,調試器也是一個*首選*選擇?嚴重的問題。 (我在上面重複了這個問題,作爲對原文的評論) – aliteralmind

0

而且你指定的東西加侖,而不是時間,然後作爲一個影響你被0

2

分我可以看到你的程序可能不會運行。您遇到的問題是與while循環

boolean done = false; 

while (done){ 
    //.....code.....// 
} 

會發生什麼事是你的while循環才能運行需要一個「真」。你的設置在開始時被設置爲false,因此循環不會被執行。你可以通過這樣做來解決這個問題

boolean done = false; 

while (!done){ 
    //....code....// 
} 

那麼這是什麼「!」基本上是這樣說的:「因爲你沒有完成這個工作,所以當你完成之後不要再次進入這個循環,只是忘記它」,或多或少;

0

我調試了你的代碼,它應該現在正常工作。 試試吧。

public static void main(String[] args) 
     { 
      //Declare Class Variables 
      String highway; 
      int startMiles=0; 
      int endMiles=0; 
      int time= 0; 
      boolean done = true; 
      while (done) 
      { 
       try 
       { 
        String start = JOptionPane.showInputDialog(null,"Enter starting mileage: "); 
        startMiles = Integer.parseInt(start); 

        String end =JOptionPane.showInputDialog(null,"Enter ending mileage: "); 
        endMiles = Integer.parseInt(end); 

        if (endMiles<=startMiles) throw new Exception(); 

        String amount = JOptionPane.showInputDialog(null,"Enter total time as a decimal value: "); 
        time = Integer.parseInt(amount); 
        done = false; 
       } 
       catch(Exception e) 
       { 
        JOptionPane.showMessageDialog(null, "Your ending mileage,"+endMiles +", must be greater than your starting mileage, " + startMiles, "Error in Mileage", JOptionPane.ERROR_MESSAGE); 
       } 
      } 
      JOptionPane.showMessageDialog(null, startMiles); 
      JOptionPane.showMessageDialog(null,"Average speed is "+ (endMiles - startMiles)/time +"miles per hour."); 
     } 
0
零(加侖)

1.By分,這就是爲什麼它給「一般的速度是無限英里每小時」

在這份聲明中有一個串聯錯誤.......,「,endMiles +」, 它應該是...... 「+ endMiles +」,

JOptionPane.showMessageDialog(null,「您的結束里程數,」,endMiles +「,必須大於您的起始里程數,」+ startMiles,「里程錯誤」,JOptionPane.ERROR_MESSAGE );