-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistanceCalc.java
More file actions
86 lines (72 loc) · 2.11 KB
/
Copy pathDistanceCalc.java
File metadata and controls
86 lines (72 loc) · 2.11 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.*;
public class DistanceCalc {
/**
* @param args
*/
static String[] city = {
"Indianapolis",
"New York",
"Tokyo",
"London",
};
static int[][] distance = {
{0, 648, 6476, 4000},
{648, 0, 6760, 3470},
{6476, 6760, 0, 956},
{4000, 3470, 5956, 0}
};
public static void main(String[] args) {
boolean keepGoing = true;
Scanner input = new Scanner(System.in);
while (keepGoing){
int from = getCity("origin");
int to = getCity("destination");
int result = 0;
result = distance[from][to];
System.out.print("The distance between ");
System.out.print(city[from]);
System.out.print(" and ");
System.out.print(city[to]);
System.out.print(" is ");
System.out.print(result);
System.out.println(" miles.");
// see if they want to play again
System.out.println("");
System.out.println("Try again? (Y/N)");
String tryAgain = input.nextLine();
tryAgain = tryAgain.toUpperCase();
char firstChar = tryAgain.charAt(0);
if (firstChar == 'Y'){
keepGoing = true;
} else {
keepGoing = false;
} // end if
} // end while
} // end main
public static int getCity(String prompt){
Scanner input = new Scanner(System.in);
int result = 0;
System.out.println(prompt);
System.out.println("");
System.out.println("0) Indianapolis");
System.out.println("1) New York");
System.out.println("2) Tokyo");
System.out.println("3) London");
System.out.println("");
System.out.println("Please select a city number");
//catch alpha input
try {
result = Integer.parseInt(input.nextLine());
} catch (Exception e){
System.out.println("Something went wrong...");
System.out.println("Assuming city 0");
result = 0;
} // end try
//catch out of range input
if ((result >= city.length) || (result < 0)){
System.out.println("Incorrect input. Assuming 0");
result = 0;
} // end if
return result;
} // end getCity
} // end clasDef