I am new to the coding world as well as this site. I have been learning Java by myself for a few days now and wanted to make a gambling program just for fun and practice.
I am going to ask two questions, one short, and one long. I'll ask the long one first! How does my code look for a beginner? I know I haven't dipped into using multiple classes yet or anything, so how does this very basic program look?
package com.company;
import java.util.Scanner;
import java.util.Random;
public class StakingGame {
static int cashStack = 500000;
static int amountStaked;
public static void main(String[] args) {
System.out.println("Welcome to my staking game.");
while(cashStack > 0) {
amountStaked();
System.out.println("Rolling...");
whoWon();
}
System.out.println("You have no more money.");
}
public static void amountStaked() {
Scanner scan = new Scanner(System.in);
System.out.println("How much would you like to stake?");
amountStaked = scan.nextInt();
System.out.println("You have chosen to stake, " + amountStaked + "$, goodluck.");
}
public static int roll() {
Random r = new Random();
int min = 0;
int max = 100;
int rolled = r.nextInt(max-min) + min;
return rolled;
}
public static void whoWon() {
int playerRoll = roll();
int computerRoll = roll();
System.out.println("You have rolled a " + playerRoll);
System.out.println("Your oppent has rolled a " + computerRoll);
if(playerRoll > computerRoll) {
stakeWon(0);
} else if (playerRoll < computerRoll) {
stakeLost(0);
} else {
System.out.println("You guys tied.");
}
}
public static int stakeWon(int newAmount) {
newAmount = cashStack + amountStaked;
cashStack = newAmount;
System.out.println("You have won the stake! You now have " + cashStack);
return cashStack;
}
public static int stakeLost(int newAmount) {
newAmount = cashStack - amountStaked;
cashStack = newAmount;
System.out.println("You have lost the stake.. You now have " + cashStack);
return cashStack;
}
}
My second question is, well, more of a question. I want this program to run until I either terminate it or the player runs out of money. So this is how I decided to go about that. I created this loop in my main method:
public static void main(String[] args) {
System.out.println("Welcome to my staking game.");
while(cashStack > 0) {
amountStaked();
System.out.println("Rolling...");
whoWon();
}
System.out.println("You have no more money.");
}
Is that the best way to make the program run until you either lose or I terminate it?