需要帮助调试,新编程

时间:2014-01-15 15:50:22

标签: java debugging

使用此代码并且无法调试它,有任何想法它应该做的是写在评论中。谢谢任何快速回答的人。任何帮助都会很棒。

/*
        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.");
        }
    }

6 个答案:

答案 0 :(得分: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 :(得分:2)

这是一个问题。

   boolean done = false;

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

答案 2 :(得分:2)

我可以看到你的程序可能甚至都没有运行。你遇到的问题是你的while循环

boolean done = false;

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

你的while循环需要一个“true”值才能运行。您在开始时将done设置为false,因此不执行循环。您可以通过执行此操作来解决此问题

boolean done = false;

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

这是做什么的“!”基本上就像是说“因为你没有这样做继续”当你完成后不要再次进入这个循环,只要忘记它“或多或少;”

答案 3 :(得分:0)

此外,您将物品分配给加仑而不是时间,然后作为效果,您将除以0

答案 4 :(得分: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.");
        }

答案 5 :(得分:0)

1.除以零(加仑),这就是“平均速度无穷大每小时英里数”的原因

在这个语句中有一个连接错误.......,“,endMiles +”, 它应该是......,“+ endMiles +”,

JOptionPane.showMessageDialog(null,“你的结束里程”,“endMiles +”,必须大于你的起始里程,“+ startMiles,”里程误差“,JOptionPane.ERROR_MESSAGE);

相关问题