-
Notifications
You must be signed in to change notification settings - Fork 0
/
tebak-angka-v2.java
94 lines (77 loc) · 2.27 KB
/
tebak-angka-v2.java
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
import java.util.Scanner;
import java.util.Random;
public class main {
//FUNGSI MEMULAI ULANG PERMAINAN
static void restart() {
Scanner ulang = new Scanner(System.in);
System.out.print("Apakah kamu ingin main lagi? (y/n): ");
String jawaban = ulang.nextLine();
if(jawaban.equalsIgnoreCase("y")) {
Random rand = new Random();
int numRand = rand.nextInt(7);
mulai(numRand);
}
else if(jawaban.equalsIgnoreCase("n")) {
System.out.println("Permainan berakhir!!");
System.out.println("\n]== TERIMA KASIH TELAH BERMAIN ==[");
}
ulang.close();
}
//FUNGSI JIKA USER SALAH DALAM MENEBAK ANGKA
static void tryAgain(int randomNum) {
Scanner ulangi = new Scanner(System.in);
System.out.print("\nSilahkan coba tebak lagi: ");
int opsi = ulangi.nextInt();
validasi(opsi, randomNum);
ulangi.close();
}
//FUNGSI VALIDASI PILIHAN USER
static void validasi(int x, int y) {
if(x > y) {
System.out.print("Tebakanmu terlalu besar!");
tryAgain(y);
}
else if(x < y) {
System.out.print("Tebakanmu terlalu kecil!");
tryAgain(y);
}
else if(x == y) {
System.out.println("Wow, tebakanmu benar!!");
restart();
}
}
//PERMAINAN DIMULAI
static void mulai(int acak) {
Scanner user = new Scanner(System.in);
System.out.print("Silahkan tebak angka dari 0 - 7: ");
int pilihanUser = user.nextInt();
validasi(pilihanUser, acak);
user.close();
}
//MAIN FUNCTION
public static void main(String[] args) {
System.out.println("== SELAMAT DATANG DI GAME TEBAK ANGKA ==");
System.out.println("\nMenu game :");
System.out.println("1. Mulai");
System.out.println("2. Keluar");
System.out.println("\n=•= By mufniDev =•=");
Scanner input = new Scanner(System.in);
System.out.print("Silahkan pilih angka menu: ");
int pilihan = input.nextInt();
if(pilihan == 1) {
Random random = new Random();
int numRandom = random.nextInt(7);
mulai(numRandom);
}
else if(pilihan == 2) {
System.out.println("Berhasil keluar dari permainan");
}
else if(pilihan > 2) {
System.out.println("Maaf, tidak bisa menemukan menu yang kamu pilih!!");
}
input.close();
}
}
//SELESAI...!!!
//SCRIPT BY MUFNI.DEV
//MY GITHUB = mufniDev