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
//попытка 2
45 changes: 44 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
import java.util.Scanner;


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

Scanner scanner = new Scanner(System.in);
Race race = new Race();

for (int i = 1; i <= 3; i++) {
String name;
int speed;
while (true) {
System.out.println("— Введите название машины № " + i + ":");
name = scanner.nextLine().trim();
if(!name.isEmpty()){
break;
}else{
System.out.println("— Название не может быть пустым!");
}

}
Comment on lines +13 to +22

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Код для считывания непустой строки с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать



while (true){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Бесконечные циклы не стоит писать, лучше всегда какое-нибудь условие поставить для повышения читабельности и надёжности

System.out.println("— Введите скорость машины №" + i + ":");
if (scanner.hasNextInt()){
speed = scanner.nextInt();
scanner.nextLine();

if(speed > 0 && speed <=250){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода

break;
}else{
System.out.println("— Неправильная скорость");
}
}else {
System.out.println("— Неправильная скорость");
scanner.nextLine();
}
}
autoCar currentCar = new autoCar(name, speed);

race.checkLeader(currentCar);
}
scanner.close();

System.out.println("Самая быстрая машина: " + race.getLeader().nameAuto);
}
}
11 changes: 11 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Race {
autoCar leader;
public void checkLeader(autoCar car){
if(leader == null || car.speedAuto > leader.speedAuto ){
leader = car;
}
}
public autoCar getLeader(){
return leader;
}
}
12 changes: 12 additions & 0 deletions src/main/java/autoCar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class autoCar {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Классы в джава и котлине принято называть с большой буквы


String nameAuto;
int speedAuto;
Comment on lines +3 to +4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Поля лучше пометить final, тем самым исключив возможность их модификации извне



autoCar(String nameAuto, int speedAuto ){
this.nameAuto = nameAuto;
this.speedAuto = speedAuto;
}

}