-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoinFlipper.java
More file actions
40 lines (32 loc) · 1.28 KB
/
CoinFlipper.java
File metadata and controls
40 lines (32 loc) · 1.28 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
import java.util.Scanner;
import java.util.Random;
public class CoinFlipper {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random rand = new Random();
String[] op = new String[5];
int numOp;
int randomNum;
System.out.println("O programa deve escolher aleatoriamente alguma opcao digitada por voce");
while (true) {
System.out.println("Selecione o numero de opçoes (até 5):");
String input = scan.nextLine();
try {
numOp = Integer.parseInt(input);
if (numOp >= 1 && numOp <= 5) {
break; // Sai do loop se o número for válido
} else {
System.out.println("Número inválido de opções!");
}
} catch (NumberFormatException e) {
System.out.println("Digite apenas números!");
}
}
for (int i = 0; i < numOp; i++) {
System.out.println("Opção " + (i + 1) + ":");
op[i] = scan.nextLine();
}
randomNum = rand.nextInt(numOp);
System.out.println("O sistema escolheu a opção: " + op[randomNum]);
}
}