forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
334 lines (273 loc) · 12.1 KB
/
Main.java
File metadata and controls
334 lines (273 loc) · 12.1 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import java.util.List;
import java.util.Scanner;
import java.util.Random;
/*
// option 1
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean playAgain;
int iteration = 1;
int maxSpeed;
do {
Race race = new Race();
if (iteration == 1) {
maxSpeed = 250;
} else {
maxSpeed = 250 + (new Random().nextInt(51));
}
for (int i = 0; i < 3; i++) {
String name;
int speed = 0;
System.out.print(" - Ââåäèòå íàçâàíèå àâòîìîáèëÿ #" + (i + 1) + ": ");
name = scanner.nextLine();
boolean validInput = false;
while (!validInput) {
System.out.print(" - Ââåäèòå ñêîðîñòü âàøåãî àâòîìîáèëÿ : ");
if(scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();
if (iteration == 1) {
if (speed > 0 && speed <= maxSpeed) {
break;
} else {
System.out.println(" — Íåïðàâèëüíàÿ ñêîðîñòü, ïîïðîáóéòå ñíîâà: ");
}
} else {
if (speed >= 250 && speed <= maxSpeed) {
break;
} else {
System.out.println(" — Íåïðàâèëüíàÿ ñêîðîñòü, ïîïðîáóéòå ñíîâà: ");
}
}
} else {
System.out.println("Ïîæàëóéñòà, ââåäèòå öåëîå ÷èñëî.");
scanner.nextLine(); // Clear the invalid input
}
}
Car car = new Car(name, speed);
race.addCar(car);
}
List<String> leaders = race.getCurrentLeaders();
if (!leaders.isEmpty()) {
System.out.println("\nÑàìàÿ áûñòðàÿ ìàøèíà: " + String.join(", ", leaders));
}
System.out.print("Õîòèòå ñûãðàòü ñíîâà? (äà/íåò): ");
String response = scanner.nextLine();
playAgain = response.equalsIgnoreCase("äà");
iteration++;
} while (playAgain);
scanner.close();
}
}
// option 2
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean playAgain;
boolean isFirstIteration = true; // Changed to a boolean
int maxSpeed;
do {
Race race = new Race();
// Adjust maxSpeed based on whether it's the first iteration
if (isFirstIteration) {
maxSpeed = 250;
} else {
maxSpeed = 250 + (new Random().nextInt(51));
}
for (int i = 0; i < 3; i++) {
String name;
int speed = 0;
System.out.print(" - Ââåäèòå íàçâàíèå àâòîìîáèëÿ #" + (i + 1) + ": ");
name = scanner.nextLine();
boolean validInput = false;
while (!validInput) {
System.out.print(" - Ââåäèòå ñêîðîñòü âàøåãî àâòîìîáèëÿ : ");
if (scanner.hasNextInt()) {
speed = scanner.nextInt();
scanner.nextLine();
if (isFirstIteration) {
if (speed > 0 && speed <= maxSpeed) {
validInput = true; // Mark the input as valid
} else {
System.out.println(" — Íåïðàâèëüíàÿ ñêîðîñòü, ïîïðîáóéòå ñíîâà: ");
}
} else {
if (speed >= 250 && speed <= maxSpeed) {
validInput = true; // Mark the input as valid
} else {
System.out.println(" — Íåïðàâèëüíàÿ ñêîðîñòü, ïîïðîáóéòå ñíîâà: ");
}
}
} else {
System.out.println("Ïîæàëóéñòà, ââåäèòå öåëîå ÷èñëî.");
scanner.nextLine(); // Clear the invalid input
}
}
Car car = new Car(name, speed);
race.addCar(car);
}
List<String> leaders = race.getCurrentLeaders();
if (!leaders.isEmpty()) {
System.out.println("\nÑàìàÿ áûñòðàÿ ìàøèíà: " + String.join(", ", leaders));
}
System.out.print("Õîòèòå ñûãðàòü ñíîâà? (äà/íåò): ");
String response = scanner.nextLine();
playAgain = response.equalsIgnoreCase("äà");
isFirstIteration = false; // Set it to false after the first iteration
} while (playAgain);
scanner.close();
}
}
// option 3:
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean playAgain;
boolean isFirstIteration = true; // Changed to a boolean
int maxSpeed;
do {
Race race = new Race();
if (isFirstIteration) {
maxSpeed = 250;
} else {
maxSpeed = 250 + (new Random().nextInt(51));
}
for (int i = 0; i < 3; i++) {
String name;
int enteredSpeed = 0;
int wrongEntries = 0;
System.out.print(" - Ââåäèòå íàçâàíèå àâòîìîáèëÿ #" + (i + 1) + ": ");
name = scanner.nextLine();
boolean validInput = false;
while (!validInput) {
System.out.print(" - Ââåäèòå ñêîðîñòü âàøåãî àâòîìîáèëÿ : ");
if (scanner.hasNextInt()) {
enteredSpeed = scanner.nextInt();
scanner.nextLine(); // Consume the newline
if (isFirstIteration) {
if (enteredSpeed > 0 && enteredSpeed <= maxSpeed) {
validInput = true; // Mark the input as valid
} else {
System.out.println(" — Íåïðàâèëüíàÿ ñêîðîñòü, ïîïðîáóéòå ñíîâà: ");
wrongEntries++;
}
} else {
if (enteredSpeed >= 250 && enteredSpeed <= maxSpeed) {
validInput = true;
} else {
System.out.println(" — Íåïðàâèëüíàÿ ñêîðîñòü, ïîïðîáóéòå ñíîâà: ");
wrongEntries++;
}
}
} else {
System.out.println("Ïîæàëóéñòà, ââåäèòå öåëîå ÷èñëî.");
scanner.nextLine();
}
}
int finalSpeed = enteredSpeed;
for (int j = 0; j < wrongEntries; j++) {
finalSpeed -= (finalSpeed * 0.1);
}
Car car = new Car(name, (int) finalSpeed);
race.addCar(car);
}
List<String> leaders = race.getCurrentLeaders();
if (!leaders.isEmpty()) {
System.out.println("\nÑàìàÿ áûñòðàÿ ìàøèíà: " + String.join(", ", leaders));
}
System.out.print("Õîòèòå ñûãðàòü ñíîâà? (äà/íåò): ");
String response = scanner.nextLine();
playAgain = response.equalsIgnoreCase("äà");
isFirstIteration = false;
} while (playAgain);
scanner.close();
}
}
*/
// option 3
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean playAgain;
boolean isFirstIteration = true; // Changed to a boolean
int maxSpeed;
do {
Race race = new Race();
StringBuilder results = new StringBuilder(); // To hold results and finalized speeds
if (isFirstIteration) {
maxSpeed = 250;
} else {
maxSpeed = 250 + (new Random().nextInt(51));
}
// Loop to collect details for 3 cars
for (int i = 0; i < 3; i++) {
String name;
int enteredSpeed = 0;
int wrongEntries = 0;
// ? ÍÅ ÄÀ¨Ì ÂÂÅÑÒÈ ÏÓÑÒÎÅ ÍÀÇÂÀÍÈÅ
do {
System.out.print(" - Ââåäèòå íàçâàíèå àâòîìîáèëÿ #" + (i + 1) + ": ");
name = scanner.nextLine().trim();
if (name.isEmpty()) {
System.out.println("Íàçâàíèå íå ìîæåò áûòü ïóñòûì. Ïîïðîáóéòå ñíîâà.");
}
} while (name.isEmpty());
boolean validInput = false;
// Input for speed with validation // ââîä ñêîðîñòè ñ ñóùåñòâóþùåé ïðîâåðêîé
while (!validInput) {
System.out.print(" - Ââåäèòå ñêîðîñòü âàøåãî àâòîìîáèëÿ: ");
if (scanner.hasNextInt()) {
enteredSpeed = scanner.nextInt();
scanner.nextLine(); // Consume the newline // ñúåäàåì ïåðåâîä ñòðîêè
// Validate the speed based on the current iteration
if (isFirstIteration) {
if (enteredSpeed > 0 && enteredSpeed <= maxSpeed) {
validInput = true; // Mark the input as valid
} else {
System.out.println(" — Íåïðàâèëüíàÿ ñêîðîñòü, ïîïðîáóéòå ñíîâà.");
wrongEntries++;
}
} else {
if (enteredSpeed >= 250 && enteredSpeed <= maxSpeed) {
validInput = true; // Mark the input as valid
} else {
System.out.println(" — Íåïðàâèëüíàÿ ñêîðîñòü, ïîïðîáóéòå ñíîâà.");
wrongEntries++;
}
}
} else {
System.out.println("Ïîæàëóéñòà, ââåäèòå öåëîå ÷èñëî.");
scanner.nextLine(); // Consume the invalid input
}
}
// Calculate the final speed based on wrong attempts
double finalSpeed = enteredSpeed;
for (int j = 0; j < wrongEntries; j++) {
finalSpeed -= (finalSpeed * 0.1); // Reduce speed by 10% for each wrong entry
}
// Create a car with the final speed
Car car = new Car(name, (int) finalSpeed);
race.addCar(car);
// Append the details to the results StringBuilder
results.append(" — Àâòîìîáèëü: ").append(name)
.append(", Ââåäåííàÿ ñêîðîñòü: ").append(enteredSpeed)
.append(", Êîëè÷åñòâî íåïðàâèëüíûõ ïîïûòîê: ").append(wrongEntries)
.append(", Êîíå÷íàÿ ñêîðîñòü: ").append((int) finalSpeed).append("\n");
}
// Find and display the current leaders after entries for all cars
List<String> leaders = race.getCurrentLeaders();
if (!leaders.isEmpty()) {
System.out.println("\nÑàìàÿ áûñòðàÿ ìàøèíà: " + String.join(", ", leaders));
}
// Display all input details after all cars have been entered
System.out.println(results);
// Ask the user if they want to play again
System.out.print("Õîòèòå ñûãðàòü ñíîâà? (äà/íåò): ");
String response = scanner.nextLine();
playAgain = response.equalsIgnoreCase("äà");
isFirstIteration = false;
} while (playAgain);
scanner.close();
}
}