-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting-Array-Python-Code
More file actions
120 lines (108 loc) · 4.7 KB
/
Sorting-Array-Python-Code
File metadata and controls
120 lines (108 loc) · 4.7 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.util.Scanner;
public class StateCapitalP {
public static void main(String[] args) {
String[][] stateCapitals = {
{"Alabama", "Montgomery"},
{"Alaska", "Juneau"},
{"Arizona", "Phoenix"},
{"Arkansas", "Little Rock"},
{"California", "Sacramento"},
{"Colorado", "Denver"},
{"Connecticut", "Hartford"},
{"Delaware", "Dover"},
{"Florida", "Tallahassee"},
{"Georgia", "Atlanta"},
{"Hawaii", "Honolulu"},
{"Idaho", "Boise"},
{"Illinois", "Springfield"},
{"Indiana", "Indianapolis"},
{"Iowa", "Des Moines"},
{"Kansas", "Topeka"},
{"Kentucky", "Frankfort"},
{"Louisiana", "Baton Rouge"},
{"Maine", "Augusta"},
{"Maryland", "Annapolis"},
{"Massachusetts", "Boston"},
{"Michigan", "Lansing"},
{"Minnesota", "Saint Paul"},
{"Mississippi", "Jackson"},
{"Missouri", "Jefferson City"},
{"Montana", "Helena"},
{"Nebraska", "Lincoln"},
{"Nevada", "Carson City"},
{"New Hampshire", "Concord"},
{"New Jersey", "Trenton"},
{"New Mexico", "Santa Fe"},
{"New York", "Albany"},
{"North Carolina", "Raleigh"},
{"North Dakota", "Bismarck"},
{"Ohio", "Columbus"},
{"Oklahoma", "Oklahoma City"},
{"Oregon", "Salem"},
{"Pennsylvania", "Harrisburg"},
{"Rhode Island", "Providence"},
{"South Carolina", "Columbia"},
{"South Dakota", "Pierre"},
{"Tennessee", "Nashville"},
{"Texas", "Austin"},
{"Utah", "Salt Lake City"},
{"Vermont", "Montpelier"},
{"Virginia", "Richmond"},
{"Washington", "Olympia"},
{"West Virginia", "Charleston"},
{"Wisconsin", "Madison"},
{"Wyoming", "Cheyenne"}
};
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a state capital and check if it is correct
System.out.print("Enter the capital of a U.S. state: ");
String userInput = scanner.nextLine();
boolean isCorrect = false;
for (String[] stateCapital : stateCapitals) {
if (userInput.equalsIgnoreCase(stateCapital[1])) {
System.out.println("\nYou are correct! " + stateCapital[0] + "'s capital is " + stateCapital[1]);
isCorrect = true;
break;
}
}
if (!isCorrect) {
System.out.println("\nSorry, that is wrong.");
}
// Display the current contents of the array
System.out.println("\nHere are all the States and their Capitals:");
for (String[] stateCapital : stateCapitals) {
System.out.println(stateCapital[0] + " - " + stateCapital[1]);
}
// Sort the array by capital using bubble sort
for (int i = 0; i < stateCapitals.length - 1; i++) {
for (int j = 0; j < stateCapitals.length - i - 1; j++) {
if (stateCapitals[j][1].compareTo
(stateCapitals[j + 1][1]) > 0) {
String[] temp = stateCapitals[j];
stateCapitals[j] = stateCapitals[j + 1];
stateCapitals[j + 1] = temp;
}
}
}
// Display the sorted contents of the array
System.out.println("\nNow here they are sorted by capital:");
for (String[] stateCapital : stateCapitals) {
System.out.println(stateCapital[1] + " - " + stateCapital[0]);
}
// Prompt the user to enter answers for all the state capitals and count the correct answers
System.out.println("\nNot sure why were doing it like this since I'm about to ask you for the Capitals, but that's what the assignment wanted so...:");
System.out.print("\nNow, bet you won't guess");
int correctCount = 0;
for (String[] stateCapital : stateCapitals) {
System.out.print("\nwhat is the capital of " + stateCapital[0] + "?");
String userAnswer = scanner.nextLine();
if (userAnswer.equalsIgnoreCase(stateCapital[1])) {
correctCount++;
}
}
// Display the total correct count
System.out.println("\nYou answered " + correctCount + " out of 50 state capitals correctly!");
System.out.println("\nNo way!!! How in the world did you manage that!");
scanner.close();
}
}