英尺到秒 - 不能增加秒Java

时间:2016-08-25 18:33:40

标签: java methods seconds

我正在制作一个程序,向用户显示他们在开车时有多少英尺的行程。

到目前为止,这是我得到的, 如果用户以60英里/小时的速度行驶,则一秒钟的注意力将会传播到88英尺。 如果用户以20MPH的速度行驶,则一秒钟的注意力将超过29英尺。等等。

我无法搞清楚的问题是,如果我在此方法上增加秒数.S.ShourstoSeconds(1)。我的程序开始分割,而不是相乘。我已经创建了新的方法,但到目前为止,我完全失去了如何使它工作。提前致谢。

  public class App {

 public App() {
    //  TODO Auto-generated constructor stub
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Conversions c = new Conversions();



    c.SetMilestoFeet(60);

    c.SetHourstoSeconds(1);


    System.out.println("Total feet travel " + c.Total() + "ft");
   }
  }

公共类转换{

static int Milestofeet =0;
static int MinutestoSeconds =0;

public Conversions() {

}



public static void SetMilestoFeet(int x)
{

    Milestofeet = x * 5280;




}

public static void SetHourstoSeconds(int hr)
{
    int hourstoMinutes = hr * 60;
     MinutestoSeconds = hourstoMinutes * 60;

        System.out.println("***********" + hr);


}

public static int Sec()
{

    return MinutestoSeconds;
}

public static int Feet()
{

    return Milestofeet;
}


public int  Total()
{
    System.out.print(Milestofeet + "   ___" + MinutestoSeconds + "______=" );

    return (Feet()/Sec());

}

}

1 个答案:

答案 0 :(得分:2)

你让这个太多太难了:

/**
 * Calculate distance traveled per second of distraction
 * User: mduffy
 * Date: 8/25/2016
 * Time: 2:40 PM
 * @link http://stackoverflow.com/questions/39152475/feet-to-distractionSeconds-cant-increment-distractionSeconds-java
 */
public class DistractionDistanceCalculator {

    public static final int FEET_PER_MILE = 5280;
    public static final int SECONDS_PER_HOUR = 3600;

    public static void main(String[] args) {
        double mph = (args.length > 0) ? Double.parseDouble(args[0]) : 60.0;
        double distractionSeconds = (args.length > 1) ? Double.parseDouble(args[1]) : 1.0;
        double distance = calculateDistractionDistance(mph, distractionSeconds);
        System.out.println(String.format("You will travel %10.3f feet if you are distracted for %10.3f distractionSeconds at %10.3f mph", distance, distractionSeconds, mph));
    }

    private DistractionDistanceCalculator() {
    }

    public static double calculateDistractionDistance(double mph, double distractionSeconds) {
        if (mph < 0.0) throw new IllegalArgumentException("Speed must be positive");
        if (distractionSeconds < 0.0) throw new IllegalArgumentException("Distraction seconds must be positive");
        return mph*FEET_PER_MILE/SECONDS_PER_HOUR*distractionSeconds;
    }
}