4/21/2011

Travel Time Calculator

這是我上禮拜的Java作業,滿分十分,老師給了九分。

以下是我寫的code

// import class
import java.util.Scanner;

public class CH03PR32
{
public static void main(String[] args)
{
// Welcome the user to come to the program
System.out.println("Welcome to the Travel Time Calculator");
System.out.println();

Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// get input numbers from the user
System.out.print("Enter miles:          ");
double miles = sc.nextDouble();
System.out.print("Enter miles per hour: ");
double milesPerHour = sc.nextDouble();
System.out.println();

// show the estimated travel time
System.out.println("Estimated travel time");

// calculate estimated hours and minutes
double hours = miles / milesPerHour ;
hours = Math.floor(hours);
int estimatedhours = (int)hours;
double minutes = 60 * (miles % milesPerHour) / milesPerHour ;
minutes = Math.floor(minutes);
int estimatedminutes = (int)minutes;

System.out.println("Hours:    " + estimatedhours);
System.out.println("Minutes:  " + estimatedminutes);
System.out.println();

// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next(); // refer to Scanner
System.out.println();
}

}
}

1 comment:

  1. 以下是老師給我的評語,可是我還是不知道怎麼修正我的程式。
    Application imported without problems and ran fine with the different tests I tried. It appears that the requirement to round minutes up to next highest minute was not implemented. Actually, it appears that what was done was to use a function of the Math class to round down rather than up.

    ReplyDelete