-
Notifications
You must be signed in to change notification settings - Fork 0
Приложение функционирует #1
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public class Auto { | ||
String name; | ||
int speed; | ||
Auto(String name, int speed) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Т.к. сам класс публичный, конструктору тоже лучше прописать модификатор |
||
this.name = name; | ||
this.speed = speed; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,29 @@ | ||
|
||
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++) { | ||
System.out.println("Введите название машины №" + i); | ||
String name = scanner.next(); | ||
int speed; | ||
while (true) { | ||
System.out.print("Введите скорость машины №" + i + "(от 0 до 250 км/ч): "); | ||
speed = scanner.nextInt(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если ввести символы вместо чисел, то здесь вылетит эксепшн, что экстренно завершит выполнение программы с потерей всех введенных данных |
||
|
||
if (speed >= 0 && speed <= 250) { | ||
scanner.nextLine(); // | ||
break; | ||
} else { | ||
System.out.println("Ошибка: Скорость должна быть в диапазоне от 0 до 250 км/ч."); | ||
} | ||
} | ||
Auto auto = new Auto(name,speed); | ||
race.newLeaderUpdate(auto); | ||
|
||
} | ||
System.out.println("Самая быстрая машина: " + race.getCurrentLeader()); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
public class Race { | ||
String leader = ""; | ||
int leaderDistance = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Эти переменные лучше сделать приватными, а для получения имени победителя написать отдельную функцию-геттер. Это исключит возможность модификации этих переменных извне, тем самым не позволяя ломать поведение этого класса, т.е. он будет защищённее и стабильнее |
||
|
||
|
||
public void newLeaderUpdate(Auto auto) { | ||
int distance = 24 * auto.speed; | ||
|
||
if (distance> leaderDistance){ | ||
leaderDistance = distance; | ||
leader = auto.name; | ||
System.out.println("Новый лидер гонки: " + leader + " проехал дистанцию: " + leaderDistance + " км."); | ||
} else { | ||
System.out.println("Лидер по прежнему: " + leader + " проехал дистанцию: " + leaderDistance + " км."); | ||
} | ||
} | ||
public String getCurrentLeader() { | ||
return leader; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Поля лучше пометить
final
, тем самым исключив возможность их модификации извне