File tree Expand file tree Collapse file tree 3 files changed +41
-0
lines changed
app/src/main/java/hexlet/code Expand file tree Collapse file tree 3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1313"GCD" https://asciinema.org/a/anWD6GPnBTcWTBLCfvbMGZeV6
1414
1515"Progression" https://asciinema.org/a/4cwJUcnV4TcEFSpdqMrIa3xjf
16+
17+ "Prime" https://asciinema.org/a/KIxIZnfYvYP0D6PokMo9QKryI
Original file line number Diff line number Diff line change 44import hexlet .code .games .Even ;
55import hexlet .code .games .Gcd ;
66import hexlet .code .games .Progression ;
7+ import hexlet .code .games .Prime ;
78import java .util .Scanner ;
89
910class 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 ;
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments