-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgambler.java
More file actions
38 lines (34 loc) · 1.07 KB
/
gambler.java
File metadata and controls
38 lines (34 loc) · 1.07 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
import java.util.Random;
import java.util.*;
public class gambler {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter how much You want to put on stake");
int stake=sc.nextInt();
System.out.println("Enter what's your goal");
int goal=sc.nextInt();
System.out.println("Please wait till we run the bets");
int trails= 2;
int bets = 0;
int wins = 0;
Random rand = new Random();
for (int t = 0; t < trails; t++) {
int cash = stake;
while (cash > 0 && cash < goal) {
bets++;
if (rand.nextInt(2) == 0) {
cash++;
} else {
cash--;
}
}
if (cash == goal) {
wins++;
}
}
double avg=1.0 * bets / trails;
System.out.println(wins + " wins of " + trails);
System.out.println("Percent of games won = " + 100.0 * wins / trails);
System.out.println("Avg number of bets = $" +avg*wins );
}
}