Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Домашнее задание 1 #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

Open
LampJuice wants to merge 2 commits into main
base: main
Choose a base branch
Loading
from dev
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
14 changes: 14 additions & 0 deletions src/main/java/Auto.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

public class Auto implements Comparable<Auto> {
String name;
int speed;


Auto(String name, int speed) {
this.name = name;
this.speed = speed;
}
public int compareTo(Auto o){
return (o.speed-speed);
}
}
48 changes: 47 additions & 1 deletion src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,52 @@

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
ArrayList<Auto> autoList = new ArrayList<>();

competitors(autoList);
Race.check(autoList);
}

public static void competitors(ArrayList<Auto> autoList) {
Scanner scanner = new Scanner(System.in);
System.out.println("Объявляем участников гонки!");
String name;

int speed = 0;
for(int i = 0; i < 3; i++){
System.out.println("Введите название автомобиля No" + (i+1));
name = scanner.next();
speed = getValidSpeed();
autoList.add(new Auto(name, speed));
Race.isWinner(name, speed);
}
}

public static int getValidSpeed () {
String spStr;
int speed = 0;
boolean flag = true;

while (flag) {
System.out.println("Введите скорость");
Scanner scanner = new Scanner(System.in);
spStr = scanner.next();
try {
speed = Integer.parseInt(spStr);
if (speed <=0 || speed > 250){
flag = true;
System.out.println("Ошибка, только целые числа от 0 да 250, еще раз...");
}else {
flag = false;
}
} catch (NumberFormatException e) {
System.out.println("Ошибка, только целые числа от 0 да 250, еще раз...");
flag = true;
}
}
return speed;
}
}
39 changes: 39 additions & 0 deletions src/main/java/Race.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.ArrayList;
import java.util.Collections;

public class Race {
static String winner;
static int start = 0;
static int winnerDistance = 0;

public static int distance24h(int speed) {
return speed * 24;
}

public static void isWinner(String name, int distance) {
if (distance > start) {
start = distance;
winner = name;
winnerDistance = distance24h(distance);
}
}

public static void check (ArrayList<Auto> checkList) {
int count = 0;
Collections.sort(checkList);
for (int i = 0; i < checkList.size()-1; i++) {
if (checkList.get(i).speed == checkList.get(i+1).speed){
count++;
}
}
if (count == (checkList.size()-1)){
System.out.println("Все финишировали одновременно!");
} else if (count == 1) {
System.out.println("Две машины: \'"+checkList.get(0).name+"\' и \'"+ checkList.get(1).name+"\' пришли одновременно, проехав "+distance24h(checkList.get(0).speed)+ " километров за 24 часа.");
} else if (count == 0) {
System.out.println("Победитель финишировал на машине: \'"+checkList.get(0).name+"\', проехав "+distance24h(checkList.get(0).speed) + " километров за 24 часа.");
} else {
System.out.println("Несколько участников пришли к финишу одновременно!");
}
}
}

AltStyle によって変換されたページ (->オリジナル) /