This repository was archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathWeek05.java
More file actions
139 lines (112 loc) · 3.53 KB
/
Week05.java
File metadata and controls
139 lines (112 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package For_Exam;
import java.util.Random;
import java.util.Scanner;
class Car {
protected int speed;
protected String name;
protected int move;
protected int second;
public Car() {
}
public Car(int speed, String name) {
this.speed = speed;
this.name = name;
}
public String getName() {
return name;
}
public int getSpeed() {
return speed;
}
public int getMove() {
return move;
}
public void setMove(int move) {
this.move = move;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
public void go() {
long seed = System.currentTimeMillis();
Random random = new Random(seed);
for (int i = 0; i < second; i++) {
if (random.nextInt(2) == 1) {
move += speed;
}
}
}
public void sayScore() {
System.out.printf("score: %d\n", move);
}
}
class SuperCar extends Car {
private long seed = System.currentTimeMillis();
private Random random = new Random(seed);
private Random random2 = new Random(seed);
private int count;
public SuperCar(int speed, String name) {
this.speed = speed;
this.name = name;
}
public void go() {
for (int i = 0; i < getSecond(); i++) {
if (random.nextInt(2) == 1) {
move += speed;
if (random2.nextInt(2) == 1) {
move += speed;
count++;
}
}
}
}
public void sayScore() {
System.out.printf("score: %d, booster: %d\n", move, count);
}
}
public class Week04 {
public static void main(String[] args) {
int speed;
String name;
int second;
int isSuper = 0;
Scanner scanner = new Scanner(System.in);
int N;
System.out.println("자동차의 갯수를 입력하세요.");
N = scanner.nextInt();
Car[] cars = new Car[N];
int count = 0;
for (int i = 0; i < cars.length; i++) {
System.out.printf("%d번 째 자동차의 스피드를 입력하세요.\n", count + 1);
speed = scanner.nextInt();
System.out.printf("%d번 째 자동차의 이름을 입력하세요.\n", count + 1);
name = scanner.next();
System.out.println("이 자동차는 슈퍼카인가요? 0 또는 1 입력");
isSuper = scanner.nextInt();
if (isSuper == 0)
cars[i] = new Car(speed, name);
else if (isSuper == 1)
cars[i] = new SuperCar(speed, name);
}
System.out.println("-----경기 참가자 소개-----");
for (int i = 0; i < cars.length; i++) {
System.out.printf("스피드는 %d이고, 이름은 %s입니다.\n", cars[i].getSpeed(), cars[i].getName());
}
System.out.println();
System.out.println("경기를 몇 초 동안 진행할까요?");
second = scanner.nextInt();
for (int i = 0; i < cars.length; i++) {
cars[i].setSecond(second);
cars[i].go();
}
System.out.println();
System.out.println("---최종 결과 발표---");
for (int i = 0; i < cars.length; i++) {
cars[i].sayScore();
}
scanner.close();
}
}