-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed-distance-time-calculator.java
58 lines (58 loc) · 1.99 KB
/
speed-distance-time-calculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.Scanner;
public class speed-distance-time-calculator
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Choose what you want to calculate:");
System.out.println("1. Speed");
System.out.println("2. Distance");
System.out.println("3. Time");
int choice = scanner.nextInt();
switch (choice)
{
case 1:
calculateSpeed();
break;
case 2:
calculateDistance();
break;
case 3:
calculateTime();
break;
default:
System.out.println("Invalid choice. Please choose 1, 2, or 3.");
break;
}
}
public static void calculateSpeed()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter distance (in kilometers): ");
double distance = scanner.nextDouble();
System.out.print("Enter time (in hours): ");
double time = scanner.nextDouble();
double speed = distance / time;
System.out.println("Speed: " + speed + " km/h");
}
public static void calculateDistance()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter speed (in km/h): ");
double speed = scanner.nextDouble();
System.out.print("Enter time (in hours): ");
double time = scanner.nextDouble();
double distance = speed * time;
System.out.println("Distance: " + distance + " kilometers");
}
public static void calculateTime()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter distance (in kilometers): ");
double distance = scanner.nextDouble();
System.out.print("Enter speed (in km/h): ");
double speed = scanner.nextDouble();
double time = distance / speed;
System.out.println("Time: " + time + " hours");
}
}