Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Проектная работа №1 #277

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Car {

private String name;
private int speed;

public Car(String name, int speed){
this.name = name;
this.speed = speed;
}

public String getName(){
return name;
}

public int getSpeed(){
return speed;
}

public int Distance(){
return speed * 24;
}
}
323 changes: 322 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,327 @@
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) {
System.out.println("Hello world!");
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;

System.out.print(" - Введите название автомобиля #" + (i + 1) + ": ");
name = scanner.nextLine();
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.toString());

// 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();
}
}
Loading