This is my first question I asked here. I am currently in CSCI 2911 in for a lab, we have to program a basic Craps game. Everything was working, until I added a simple betting system. Once I implemented it, it doesn't work.
Here's the code for my java classes:
Main class:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package rjohncraps;
import java.util.Scanner;
/**
*
* @author Randall
*/
public class RJohnCraps {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
// Game Object
Game g = new Game ();
// Win Initalization
int wins = 0;
// Money Initlization
int money = 0;
// Bet Amount Initialization
int bet = 0;
// GameInput Initialization
String gameInput = " ";
System.out.println("Want to play a game of Craps? (y/n)");
gameInput = input.nextLine();
// If gameInput is yes or no
switch (gameInput) {
case "Y":
case "y":
case "Yes":
case "yes": //If yes.
// Game do-while
do {
do { // Keep asking until bet is more or equal to zero
System.out.println("How much do you want to bet?");
bet = input.nextInt();
} while (bet <= 0);
// return character in play() [Game.java] to char result
char result = g.play();
switch (result) {
case 'W': // If Wins
System.out.println("You won!");
wins = wins + 1; // Add One Win to Win Count
money = money + bet; // Add Bet Amount to Total Money Amount
break;
case 'L': // If Loses
System.out.println("You lose!");
money = money - bet; // Deduct Bet Amount to Total Money Amount
break;
}
System.out.println("Do you want to play again? (y/n)"); // Ask to play again
gameInput = input.nextLine();
// If gameInput = yes, then it will repeat
} while (gameInput.equals("y") || gameInput.equals("Y"));
// If gameInput = no
System.out.println("Your total wins is "+wins+" wins.");
// It total money is less than zero.
if (money < 0) {
// If it's zero, then it's a negative, so multiply money by -1 to be positive
money = money * -1;
System.out.println("You owe "+money+" dollars.");
} else if (money > 0) { // If total money is more than zero
System.out.println("You earned "+money+" dollars.");
} else { // If total money is zero.
System.out.println("You earned nothing.");
}
break;
// If Don't want to play
case "N":
case "n":
case "No":
case "no":
// Print a nice message
System.out.println("Okay, have a nice day!");
break;
// If Default, Print Invalid Message
default:
System.out.println("Invalid Word");
}
}
}
Game class:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package rjohncraps;
/**
*
* @author Randall
*/
public class Game {
// Int Variables (set to private)
private int result;
private int point;
// Craps Play
public char play() {
// Dice Object from [Dice class]
Dice d = new Dice();
// Blank Char
char blank = ' ';
// return int from rollTwoDice() to int result
result = d.rollTwoDice();
// Print the results from the Dice Rolls
System.out.print(result+", ");
// If First Roll is 2,3,12
if (result == 2 || result == 3 || result == 12) {
// Return L char
return 'L';
}
// If First Roll 7 and 11
else if (result == 7 || result ==11) {
// Return W char
return 'W';
// If neither 2,3,12,7,11
} else {
// Assigned Result to Point variable
point = result;
// Loop until Result equals 7 or Point (First Roll)
do {
// return integer from rollTwoDice() to result variable
result = d.rollTwoDice();
// print result
System.out.print(result+", ");
} while (result != 7 && result != point);
// if result equals 7
if (result == 7) {
// Return char L
return 'L';
}
// if result equals point (first roll)
if (result == point) {
// return W char
return 'W';
}
}
return blank;
}
}
Dice class: (Since I cannot post more than 2 links, I will post the code for Dice class here, since it's the shortest).
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package rjohncraps;
import java.util.Random;
/**
*
* @author Randall
*/
public class Dice {
// Int Variable for dice one, dice two, and sum of d1 and d2
private int d1;
private int d2;
private int sum;
// Random object
Random bag = new Random();
// Rolling Dice Method
public int rollTwoDice() {
// Sum of Two Dice Initialization
int sum = 0;
// Generate random numbers for dice one and dice two
d1 = bag.nextInt(6)+1;
d2 = bag.nextInt(6)+1;
// Calculate the sum of dice 1 and dice 2
sum = d1 + d2;
// Return sum
return sum;
}
}
Here is what's going on (The Problem):
When I run the code (from the main class), First it will ask if I want to play Craps, in which I will type "y" from my keyboard.
Then it will ask how much I want to bet, in which I put in a integer amount that is positive. for instance, 100.
After that it will print the result from the dice rolls, i got 8, 9, 8. If you know the game of Craps, that means I won.
It suppose to say "Do you want to play again? (y/n)," in which I suppose to type either "y" or "n", but instead it will ignore the keyboard input part, and it will just print the total wins I got and the amount I earned.
Here's what the output looks like:
Want to play a game of Craps? (y/n)
y
How much do you want to bet?
100
8, 9, 8, You won!
Do you want to play again? (y/n)
// I suppose to type something in the keyboard,
// but it skips that and just prints the results.
Your total wins is 1 wins.
You earned 100 dollars.
BUILD SUCCESSFUL (total time: 1 minute 14 seconds)
I don't know what's going on, it seems like the do while that labeled (Commented "Game Do-While" doesn't seem to work. What do you guys think? I have no idea.
If you need more information, just let me know!