Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Пустой репозиторий для работы с Java кодом в Android Studio
# Проектная работа №1
9 changes: 9 additions & 0 deletions src/main/java/Cars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Cars {
String name;
int speed;

public Cars(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
38 changes: 37 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);

// Это наш гараж
ArrayList<Cars> carsInRace = new ArrayList<>();

// Запрашиваем 3 машины
for(int i = 1; i <= 3; i++) {
System.out.println("Введите название машины " + i);
String name = scanner.nextLine();
carsInRace.add(new Cars(name, getSpeed(i)));
}

// Нфчинаем гонку!
Race race = new Race(carsInRace);
System.out.println("Самая быстрая машина: " + race.leader.name + ", проехала "
+ (race.leader.speed * 24) + " км за 24 часа");
}

public static int getSpeed(int num) {
Scanner scanner = new Scanner(System.in);

while(true) {
System.out.println("Введите скорость машины " + num + " (от 1 до 250)");
int speed = scanner.nextInt();

if (speed < 0) {
System.out.println("Ошибка: скорость должна быть больше 0!");
}
else if (speed > 250) {
System.out.println("Ошибка: скорость должна быть меньше 250!");
}
else {
return speed;
}
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.ArrayList;

public class Race {
Cars leader;

public Race(ArrayList<Cars> allCars) {
int totalPath = 0;

for (Cars car : allCars) {
int path = car.speed * 24;
if (totalPath < path) {
totalPath = path;
leader = car;
}
// System.out.println("Машина " + car.name + ", скорость " + car.speed);
}
}
}