Write a java program that simulates Gambler. The gambler gets in stake and goal.
ID: 3592798 • Letter: W
Question
Write a java program that simulates Gambler. The gambler gets in stake and goal. Each time he play he will move forward 1 or backwards 1. Each time new value is created. The program should store that value in array. The problem here is that we don't know how many values there will be created. So we need to create array big enough, lets say it can take 1000000 values. If it takes more then it will crash.
Use the following code as a skeleton for javaprogram!
public static void main(String[] args) {
int stake = Integer.parseInt(args[0]); // gambler's stating bankroll
int goal = Integer.parseInt(args[1]); // gambler's desired bankroll
int cash = stake;
while (cash > 0 && cash < goal) {
bets++;
if (Math.random() < 0.5) cash++; // win $1
else cash--; // lose $1
}
--------------------------------------------------------------------
Can you guys also please stop posting weird or random answers? If you can't answer the question just let it pass? It has been third time I just get weird with random answers. I really need help with this problem.
Explanation / Answer
Below is the desire code for the given problem:
public class Gambler {
public static void main(String[] args) {
int stake = Integer.parseInt(args[0]); // gambler's stating bankroll
int goal = Integer.parseInt(args[1]); // gambler's desired bankroll
int bets = 0;
//Creating array to store cash values after each bet
//size is enough big here
int[] cashValues = new int[1000000];
//counter to keep track of index of cash values array
int count = 0;
int cash = stake;
while (cash > 0 && cash < goal) {
bets++;
if (Math.random() < 0.5) {
cash++; // win $1
}
else {
cash--; // lose $1
}
//storing cash value after bet into cashValues array
cashValues[count++] = cash;
}
Please ask your queries in comment section if you have any.
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.