到目前为止,这是我需要获取的每小时英里数,该数字后面没有小数,而我仍然坚持这样做。任何帮助将不胜感激。

附言对不起,如果我第一次张贴这个错误

import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;

public class MilesPerHour
{
    private int distance, hours, minutes;
    private double mph;

    public MilesPerHour()
    {
        setNums(0,0,0);
        mph=0;
    }

    public MilesPerHour(int dist, int hrs, int mins)
    {
        distance = dist;
        hours = hrs;
        minutes = mins;

    }

    public void setNums(int dist, int hrs, int mins)
    {


    }

    public void calcMPH()
    {

        mph = (distance)/(hours + (minutes/60.0));

    }

    public void print()
    {
        System.out.println(distance + " miles in " + hours + " hours and " + minutes + " minutes = " + mph + "MPH.");

    }

    public static void main( String[] args )

    {

    Scanner keyboard = new Scanner(in);

        out.print("Enter the distance :: ");
        int dist = keyboard.nextInt();

        out.print("Enter the hours :: ");
        int hrs = keyboard.nextInt();

        out.print("Enter the minutes :: ");
        int mins = keyboard.nextInt();

        MilesPerHour test = new MilesPerHour(dist, hrs, mins);
        test.calcMPH();
        test.print();

最佳答案

您可以这样做:

public void calcMPH()
{

    mph = Math.round((distance)/(hours + (minutes/60.0)));
}

关于java - 每小时英里数计算器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34168817/

10-10 07:16