Write a program to input speed in mph and time in hours and
Write a program to input speed (in mph) and time (in hours) and calculate distance=speed*time.
-->For Example: Sample data: if the user enters a speed of 65 mph and a time of 1.25 hours then distance=81.25 miles.
-Use input validation to make sure speed and time are positive.
Solution
DistanceTest.java
import java.util.Scanner;
public class DistanceTest {
public static void main(String[] args) {
double speed = 0;
double time = 0;
Scanner scan = new Scanner(System.in);
while(speed<=0){
System.out.println(\"Enter the speed (in mph): \");
speed = scan.nextDouble();
}
while(time<=0){
System.out.println(\"Enter the time (in hours): \");
time = scan.nextDouble();
}
double distance=speed*time;
System.out.println(\"distance=\"+distance+\" miles.\");
}
}
Output:
Enter the speed (in mph):
65
Enter the time (in hours):
1.25
distance=81.25 miles.
