Skip to content

Commit d8a7c5d

Browse files
committed
Edit App and README, add Prime game
1 parent dad2012 commit d8a7c5d

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
"GCD" https://asciinema.org/a/anWD6GPnBTcWTBLCfvbMGZeV6
1414

1515
"Progression" https://asciinema.org/a/4cwJUcnV4TcEFSpdqMrIa3xjf
16+
17+
"Prime" https://asciinema.org/a/KIxIZnfYvYP0D6PokMo9QKryI

app/src/main/java/hexlet/code/App.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import hexlet.code.games.Even;
55
import hexlet.code.games.Gcd;
66
import hexlet.code.games.Progression;
7+
import hexlet.code.games.Prime;
78
import java.util.Scanner;
89

910
class App {
@@ -14,6 +15,7 @@ public static void main(String[] args) {
1415
System.out.println("3 - Calculator");
1516
System.out.println("4 - GCD");
1617
System.out.println("5 - Progression");
18+
System.out.println("6 - Prime");
1719
System.out.println("0 - Exit");
1820

1921
Scanner scanner = new Scanner(System.in);
@@ -36,6 +38,9 @@ public static void main(String[] args) {
3638
case "5":
3739
Progression.startGame();
3840
break;
41+
case "6":
42+
Prime.startGame();
43+
break;
3944
case "0":
4045
System.out.println("Goodbye!");
4146
break;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package hexlet.code.games;
2+
3+
import hexlet.code.Engine;
4+
import java.security.SecureRandom;
5+
6+
public class Prime {
7+
public static boolean isPrime(int number) {
8+
if (number < 2) {
9+
return false;
10+
}
11+
for (int i = 2; i < number; i++) {
12+
if (number % i == 0) {
13+
return false;
14+
}
15+
}
16+
return true;
17+
}
18+
19+
public static void startGame() {
20+
String description = "Answer 'yes' if given number is prime. Otherwise answer 'no'.";
21+
String[][] data = new String[Engine.getRoundsCount()][2];
22+
SecureRandom random = new SecureRandom();
23+
24+
for (int i = 0; i < Engine.getRoundsCount(); i++) {
25+
int number = random.nextInt(99) + 1;
26+
String question = String.valueOf(number);
27+
String correctAnswer = isPrime(number) ? "yes" : "no";
28+
29+
data[i][0] = question;
30+
data[i][1] = correctAnswer;
31+
}
32+
Engine.run(description, data);
33+
}
34+
}

0 commit comments

Comments
 (0)