I have an assignment for my programming course. I have only one issue which I cannot figure out.
My instructions are the following: Write a program that simulates how often a player would win if they rolled the dice 100 times. If the players rolls a 7, or 11 they win
My Problem is the following: My program is showing that I'm winning more times than I should be. I only want my program to print out the message that the user has won, if they have a seven or an eleven, but that isn't happening. Could anyone give me some advice on what I might need to do?
Below is my code. Thank you very much in advance for your help.
@author Jordan Navas
@version 1.0
COP2253 Workshop7
File Name: Craps.java
*/
import java.util.Random;
public class Craps
{
public static void main(String[] args)
{
Random rand = new Random();
int gamesWon=0;
int gamesLost=0;
for(int i=1;i<=100;i++)
{
craps(rand);
if(craps(rand))
{
gamesWon++;
}
else{
gamesLost++;
}
}
System.out.println("Games You Have Won: " + gamesWon);
System.out.println("Games You Have Lost: " + gamesLost);
}
public static boolean craps(Random rand)
{
int firstDice = rand.nextInt(6)+1;
int secondDice = rand.nextInt(6+1);
int sumOfDies = firstDice + secondDice;
System.out.print("[" + firstDice + "," + secondDice + "]");
if (sumOfDies == 7 || sumOfDies == 11)
{
System.out.println(sumOfDies + " You Won! Congratulations! You Won! Congratulations! ");
return true;
} else if(sumOfDies == 2 || sumOfDies == 3 || sumOfDies == 12)
{
System.out.println(sumOfDies + " Congratulations! You Lost! ");
return false;
}
int point = sumOfDies;
System.out.print("Point: " + point + " ");
if (sumOfDies == point)
{
System.out.println(sumOfDies + " You Won! Congratulations! You Won! Congratulations!");
return true;
} else
{
System.out.println(sumOfDies + " Congratulations! You Lost! ");
return false;
}
}
}
Aucun commentaire:
Enregistrer un commentaire