Unable to end this dice game properly - java

I am trying to end this game properly but the do while loop or something I am doing is causing the game to keep going. If someone has further advice for me to end this game after either score reaches 99 I would appreciate it. I have pasted both classes sources. I am also student so do forgive me.
class
package programming2;
import java.util.Random;
import java.util.Scanner;
public class Matador {
public static void main(String[] args) {
/*The game is played between the player and the computer.
The player's score and the computer's score each begin at 0.
The object of the game is to be the first reach 99 points.
The player and the computer take turns. On each turn: */
Scanner scnr = new Scanner(System.in);
Die matador = new Die();
int playerScore = 0;
int computerScore = 0;
int playerPick = 0;
int cpu;
int turn;
Random randGen = new Random();
cpu = randGen.nextInt(6) + 2;
int cpuPick = 0;
int quit = 0;
System.out.println("Welcome to el Matador!");
System.out.println("The winner of this game is the first to score 99 points");
do {System.out.println("Player 1 please pick your number"); // start of master loop
playerPick = scnr.nextInt();
System.out.println("Player 1 you selected " + playerPick);
System.out.println("Player 2 please pick your number");
cpuPick = cpu;
System.out.println("Player 2 selects " + cpuPick);
//
do { // start of game loop - master do loop player 1
if (computerScore > 98) {
quit = 1;
break;
}
System.out.println("Player 1 rolls dice");
matador.rollDieNow();
System.out.println("Values for 1st dice is: " + matador.getValueDie1() + " and " + matador.getValueDie2());
System.out.println("Combined values are " + matador.getValue());
turn = 0; // start of loop marker for player 1
if (matador.getValueDie1() == 1 && matador.getValueDie2() == 1) {
playerScore = playerScore + -25;
break;
}
if (matador.getValueDie1() == 1 || matador.getValueDie2() == 1) {
playerScore = playerScore + 0;
System.out.println("Changing Turns!");
break;
}
if (matador.getValueDie1() != 1 || matador.getValueDie2() != 1) { // sum of dicevalue added to score
playerScore = playerScore + matador.getValue();
}
if (playerPick == matador.getValueDie1() || playerPick == matador.getValueDie2()) { // if picked number appears on at least 1
turn = turn + 1;
}
if (playerPick == matador.getValueDie1() && playerPick == matador.getValueDie2()) { // if picked number appears on both dice
System.out.println("You are the winner player 1 you matched your numbers!!");
System.out.print("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
quit = 1;
break;
}
System.out.println("Player 1 Score: " + playerScore);
}
while (turn < turn + 1 || quit != 1);
// turn changes and also end of loop marker
System.out.println("Score update! Player 1 score is: " + playerScore + " |-| player 2 score is " + computerScore);
turn = 0; // turn counter resets
do { // start of computer player's turn
if (playerScore > 98) {
quit = 1;
break;
}
System.out.println("Player 2 rolls dice");
matador.rollDieNow();
System.out.println("Values for 1st dice is: " + matador.getValueDie1() + " and " + matador.getValueDie2());
System.out.println("Combined values are " + matador.getValue());
if (matador.getValueDie1() == 1 && matador.getValueDie2() == 1) { // loop marker for cpu starts
computerScore = computerScore + -25;
break;
}
if (matador.getValueDie1() == 1 && matador.getValueDie2() == 1) { // loses turns if 1 appears on both sides
computerScore = computerScore + 0;
System.out.println("Changing Turns!");
break;
}
if (matador.getValueDie1() != 1 || matador.getValueDie2() != 1) { // computer scores value of dice
computerScore = computerScore + matador.getValue();
if (computerScore > 98) {
break;
}
}
if (cpuPick == matador.getValueDie1() || cpuPick == matador.getValueDie2()) {
turn = turn + 1;
}
if (cpuPick == matador.getValueDie1() && cpuPick == matador.getValueDie2()) {
System.out.println("You are the winner player 2 you matched your numbers!!");
System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
quit = 1;
break;
}
System.out.println("Player 2 Score: " + computerScore);
}
while (playerScore < 98 || computerScore <98 || quit != 1);
}
while (playerScore < 98 || computerScore < 98 || quit != 1); // end of master loop
System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
}
}
other class if needed but it has nothing to do with the score as its mainly for determining some values:
package programming2;
import java.util.Random;
import java.util.Scanner;
public class Die {
private int die1; //instance variable
private int die2;
private int rolledDice;
public Die() { //constructor assigns random value
die1 = (int) (Math.random()*6)+1;
die2 = (int) (Math.random()*6)+1;
}
public void rollDieNow () { //rolls dice
Random randGen = new Random();
die1 = randGen.nextInt(6) + 1;
die2 = randGen.nextInt(6) + 1;
}
public int getValue() { // gets dice value
rolledDice = die1 + die2;
return rolledDice;
}
public int getValueDie1() { // gets die1 value
return die1;
}
public int getValueDie2() { // gets die2 value
return die2;
}
}

dice class
package finalproject2;
import java.util.Random;
public class Die {
Random randGen = new Random();
private int value; // instance variable -> stores the roll
// each Die object gets its own copy of the instance variable
public Die() { // constructor rolls dice (calls rollDieNow() method)
rollDieNow();
}
public void rollDieNow() { // rolls dice
value = randGen.nextInt(6) + 1;
}
public int getValue() { // gets dice value
return value;
}
}
Matador Class
package finalproject2;
import java.util.Random;
import java.util.Scanner;
public class Matador {
public static void main(String[] args) {
/*The game is played between the player and the computer.
The player's score and the computer's score each begin at 0.
The object of the game is to be the first reach 99 points.
The player and the computer take turns. On each turn: */
Scanner scnr = new Scanner(System.in);
Die d1 = new Die();
Die d2 = new Die();
int playerScore = 0;
int computerScore = 0;
int playerPick = 0;
int cpu;
int turn;
int sum ;
Random randGen = new Random();
cpu = randGen.nextInt(6) + 2;
int cpuPick = 0;
int quit = 0;
System.out.println("Welcome to el Matador!");
System.out.println("The winner of this game is the first to score 99 points");
// use System.exit(0) to stop program execution
do {
if (playerScore >= 99 || computerScore >= 99) {
System.out.println("Game Over!");
System.exit(0);
}
System.out.println("Player 1 please pick your number"); // start of master loop
playerPick = scnr.nextInt();
System.out.println("Player 1 you selected " + playerPick);
System.out.println("Player 2 please pick your number");
cpuPick = cpu;
System.out.println("Player 2 selects " + cpuPick);
//
do { // start of game loop - master do loop player 1
System.out.println("Player 1 rolls dice");
d1.rollDieNow();
d2.rollDieNow();
sum = d1.getValue() + d2.getValue() ;
System.out.println("Values for both dice are: " + d1.getValue() + " and " + d2.getValue());
System.out.println("Combined values are " + sum);
turn = 0; // start of loop marker for player 1
if (d1.getValue() == 1 && d2.getValue() == 1) {
playerScore = playerScore + -25;
System.out.println("Ouch! Lost 25 points!");
break; // lost of turn, loop ends
}
if (d1.getValue() == 1 || d2.getValue() == 1) {
playerScore = playerScore + 0;
System.out.println("Changing Turns!");
break; // lost of turn loop ends
}
if (d1.getValue() != 1 || d2.getValue() != 1) { // sum of dice value added to score
playerScore = playerScore + (d1.getValue() + d2.getValue());
System.out.println("Gained points! " + (d1.getValue() + d2.getValue()));
}
if (playerPick == d1.getValue() || playerPick == d2.getValue()) { // if picked number appears on at least 1
turn = turn + 1;
}
if (playerPick == d1.getValue() && playerPick == d2.getValue()) { // if picked number appears on both dice
System.out.println("You are the winner player 1 you matched your numbers!!");
System.out.print("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
System.exit(0); // game ends completely
}
System.out.println("Player 1 Score: " + playerScore);
}
while (turn < 1);
// turn changes and also end of loop marker
System.out.println("Score update! Player 1 score is: " + playerScore + " |-| player 2 score is " + computerScore);
turn = 0; // turn counter resets
do { // start of computer player's turn
if (playerScore >= 99 || computerScore >= 99) {
System.out.println("Game Over!");
System.exit(0);
}
System.out.println("Player 2 rolls dice");
d1.rollDieNow();
d2.rollDieNow();
System.out.println("Values for 1st dice is: " + d1.getValue() + " and " + d2.getValue());
System.out.println("Combined values are " + (d1.getValue() + d2.getValue()));
if (d1.getValue() == 1 && d2.getValue() == 1) { // loses points and turn
computerScore = computerScore + -25;
System.out.println("Ouch! Lost 25 points!");
break;
}
if (d1.getValue() == 1 || d2.getValue() == 1) { // loses turns if 1 appears on both sides
computerScore = computerScore + 0;
System.out.println("Changing Turns!");
break;
}
if (d1.getValue() != 1 || d2.getValue() != 1) { // computer scores value of dice
computerScore = computerScore + (d1.getValue() + d2.getValue());
System.out.println("Gained points! " + (d1.getValue() + d2.getValue()));
}
if (cpuPick == d1.getValue() || cpuPick == d2.getValue()) {
turn = turn + 1;
}
if (cpuPick == d1.getValue() && cpuPick == d2.getValue()) {
System.out.println("You are the winner player 2 you matched your numbers!!");
System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
System.exit(0);
}
System.out.println("Player 2 Score: " + computerScore);
}
while (turn < 1); // for some reason on my previous revision this was set to
// a different condition than player 1, now the game ends appropiate
}
while (playerScore <= 99 || computerScore <= 99); // end of master loop
System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
}
}

Related

I have to enter twice for code to complete

I am working on a Pig Game. I have two submit an answer twice for my code to work. For example. if i want to roll the dice, i have to submit r two times for it to roll. I can not find an instance where i am printing the same thing twice, and its been constantly in my code since i started working on the project. Any ideas?
The only time it does not have this issue is on the first run after a turnover.
import java.util.Random;
import java.util.Scanner;
public class Project3_Part1_Final {
static int playerScore = 0;
static int playerTotal = 0;
static int dice = 0;
static final int FINAL_SCORE = 20;
static int playerTwoScore = 0;
static int playerTwoTotal = 0;
static boolean gameOver = false;
static boolean turnOver = false;
static char repeat;
static String input;
static Scanner key = new Scanner(System.in);
static Random rand = new Random();
public static void main(String[] args) {
PlayerOneTurn();
PlayerTwoTurn();
if (playerTotal >= FINAL_SCORE) {
System.out.print("Current Score: Player 1 has " + playerTotal);
System.out.print(", Player 2 has " + playerTwoTotal);
System.out.println("Player 1 wins!");
}
if (playerTwoTotal >= FINAL_SCORE) {
System.out.print("Current Score: Player 1 has " + playerTotal);
System.out.print(", Player 2 has " + playerTwoTotal);
System.out.println("Player 1 wins!");
}
}
public static void PlayerOneTurn() {
//System.out.print("Player 1 turn total is " + playerTotal); // welcome line
//System.out.print(". Enter (r)oll or (s)top: ");
while (turnOver == false) {
do {
System.out.print("Player 1 turn total is " + playerTotal); // welcome line
System.out.print(". Enter (r)oll or (s)top: ");
input = key.next(); // input = next key entered
repeat = input.charAt(0);
if (repeat == 'r') // if input letter = r
{
dice = rand.nextInt(6) + 1; // set dice to number between 1-6
System.out.println("Player 1 rolled: " + dice); // display number, signifying the amount rolled
if (dice == 1) // if the number rolled happens to be 1...
{
playerScore = 0; // reset score to 0
System.out.print("Player 1 lost their turn! ");
System.out.print("Player 1 total is " + playerTotal);
turnOver = true; // go to next player.
break;
}
else {
playerScore += dice; // add dice amount to player score
System.out.print("Player 1 turn total is " + playerScore);// print out total amount earned //
// after last roll
System.out.print(" Enter (r)oll or (s)top: "); // repeat question.
input = key.next();
repeat = input.charAt(0);
if (playerScore >= 20) {
playerTotal = playerScore;
gameOver = true;
}
}
} else if (repeat == 's') // if neither option is called
{
break;
}
else {
System.out.println("Incorrect entry, please try again"); // prompt retry
input = key.next();
}
} while (turnOver == false || dice != 1);
playerTotal += playerScore;
playerScore = 0;
if (playerTotal >= FINAL_SCORE) {
// System.out.println("YOU WIN!");
gameOver = true;
while (playerTotal >= FINAL_SCORE)
;
break;
}
}
}
public static void PlayerTwoTurn() {
System.out.print("Player 2 turn total is " + playerTwoTotal); // welcome line
System.out.print(". Enter (r)oll or (s)top: ");
while (turnOver == false) {
do {
input = key.next(); // input = next key entered
repeat = input.charAt(0);
if (repeat == 'r') // if input letter = r
{
dice = rand.nextInt(6) + 1; // set dice to number between 1-6
System.out.println("Player 2 rolled: " + dice); // display number, signifying the amount rolled
if (dice == 1) // if the number rolled happens to be 1...
{
playerScore = 0; // reset score to 0
System.out.print("Player 2 lost their turn! ");
System.out.println("their total is " + playerTwoTotal);
turnOver = true; // go to next player.
while (playerTwoTotal < 20)
; // if the playerTotal is under 20
}
else {
playerTwoScore += dice; // add dice amount to player score
System.out.print("Player 1 turn total is " + playerTwoScore);// print out total amount earned //
// after last roll
System.out.print(" Enter (r)oll or (s)top: "); // repeat question.
input = key.next();
repeat = input.charAt(0);
}
} else if (repeat != 's') // if neither option is called
{
break;
}
else {
System.out.println("Incorrect entry, please try again"); // prompt retry
input = key.next();
}
} while (turnOver == false || dice != 1);
playerTwoTotal += playerTwoScore;
System.out.println("Player 2 score is " + playerTwoTotal);
playerTwoScore = 0;
if (playerTwoTotal >= FINAL_SCORE) {
// System.out.println("YOU WIN!");
gameOver = true;
while (playerTwoTotal >= FINAL_SCORE)
;
break;
}
}
}
}
Output:
Current Score: Player 1 has 0, Player 1 has 0
Player 1 turn total is 0. Enter (r)oll or (s)top: r
You rolled: 4
Player 1 turn total is 4 Enter (r)oll or (s)top: r
r
You rolled: 5
Player 1 turn total is 9 Enter (r)oll or (s)top: r
r
You rolled: 1
Turn over
Current Score: Player 1 has 0, Player 2 has 0
Player 2 turn total is 0. Enter (r)oll or (s)top: r
You rolled: 1
Turn over
Current Score: Player 1 has 0, Player 1 has 0
Player 1 turn total is 0. Enter (r)oll or (s)top: r
You rolled: 3
Player 1 turn total is 3 Enter (r)oll or (s)top: r
r
You rolled: 6
Player 1 turn total is 9 Enter (r)oll or (s)top:

Stuck in loop making war game

I'm making a war card game. I'm only supposed to use one class. When I run this it assigns card1 and card2 values properly, runs the loops and prints fine, but it only assigns RNG values once and I need it to break the loop and reassign values each time until someone wins.
I tried breaking it but that only ends the entire code. I'm not looking for an answer, just some input and help. It should play a hand, deduct one point from the loser add to the winner, unless it's a tie. Then it runs War and deducts 3 and adds 3 instead (for the amount of cards used). One hits zero, the game stops and the winner is printed.
import java.util.Random;
public class warGame {
public static void main(String[] args) {
String p1 = "Player 1";
String p2 = "Player 2";
int s = 10;
int p1Score = s;
int p2Score = s;
Random card = new Random();
int card1 = card.nextInt(14);
int card2 = card.nextInt(14);
int t = 1;
int war1 = card.nextInt(14);
int war2 = card.nextInt(14);
System.out.println("Let's play War!");
//Do while loop that runs the entirety of the program inside.
do {
System.out.println("Turn " + t++ + " -- Player 1's card: " + card1 + " Player 2's card: " + card2);
//determines cards, calculates values and runs proper loop
if (card1 > card2) {
System.out.println("Player 1 wins!");
p1Score += 1;
p2Score -= 1;
System.out.println("Scores -- " + "Player 1: " + p1Score + " Player 2: " + p2Score);
} else if (card1 == card2) {
System.out.println("Time for war!");
System.out.println("Player 1's war card is: " + war1 + " Player 2's war card is: " + war2);
if (war1 > war2) {
System.out.println("Player 1 wins the war!");
p1Score += 3;
p2Score -= 3;
} else {
System.out.println("Player 2 wins the war!");
p1Score -= 3;
p2Score += 3;
}
} else {
System.out.println("Player 2 wins!");
p1Score -= 1;
p2Score += 1;
System.out.println("Scores -- " + "Player 1: " + p1Score + " Player 2: " + p2Score);
}
} while (p1Score > 0 && p2Score > 0);
//The following will print out the winner and end the game.
if (p1Score == 0) {
System.out.println("Player 2 wins the game!");
} else {
System.out.println("Player 1 wins the game!");
}
}
}
If you want the loop to have different outcomes, you need to provide it with different values.
When you do:
int card1 = card.nextInt(14);
int card2 = card.nextInt(14);
int war1 = card.nextInt(14);
int war2 = card.nextInt(14);
You're assigning random values to those four variables. However, these values are never changed.
I would replace these lines with:
int card1;
int card2;
int war1;
int war2;
And then:
do {
// update card values
card1 = card.nextInt(14);
card2 = card.nextInt(14);
war1 = card.nextInt(14);
war2 = card.nextInt(14);
System.out.println("Turn " + t++ + " -- Player 1's card: " + card1 + " Player 2's card: " + card2);
This time the variables' values will be different on each iteration.
Simple answer actually:
You assign the values of the cards outside your do-while loop. That means those values will never change unless you change them inside the loop.

Simulating Game of Craps in JAVA

I am new to Java and was trying to learn by doing some excercises that I found online. So please excuse me if this is too naive.
This excercise was about writing a program for game of craps with the following rules:
In the game of craps, a pass line bet proceeds as follows: Two six-sided dice are
rolled; the first roll of the dice in a craps round is called the “come out roll.”
A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12
automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number
becomes “the point.” The player keeps rolling the dice until either 7 or the point is
rolled. If the point is rolled first, then the player wins the bet. If a 7 is rolled first,
then the player loses.
Write a program that simulates a game of craps using these rules without human
input. Instead of asking for a wager, the program should calculate whether the
player would win or lose. The program should simulate rolling the two dice and
calculate the sum. Add a loop so that the program plays 10,000 games. Add
c ounters that count how many times the player wins, and how many times the
player loses. At the end of the 10,000 games, compute the probability of winning
[i.e., Wins / (Wins + Losses)] and output this value. Over the long run, who
is going to win the most games, you or the house?
Here is the code that I have written :
// GAME OF CRAPS
public static void main (String[] args)
{
int dice1 = 0;
int dice2 = 0;
int scorenew = 0;
int point = 0;
int wins = 0;
int loss = 0;
for (int i = 0; i < 10000; i++)
{
System.out.println ("roll the dices");
int score = roll (dice1, dice2);
System.out.println ("\n score " + score);
if (score == 11 || score == 7)
{
System.out.println ("\n Score = " + score);
System.out.println ("you win");
wins = wins + 1;
}
if (score == 2 || score == 3 || score == 12)
{
System.out.println ("\n Score = " + score);
System.out.println ("you lose");
loss = loss + 1;
}
else if (score == 4 || score == 5 || score == 6 || score == 8 || score == 9 || score == 10)
{
point = point + score;
System.out.println ("\n Point = " + point);
do
{
scorenew = roll (dice1, dice2);
System.out.println ("\n Score new = " + scorenew);
if (scorenew == point)
{
System.out.println ("\n you win");
wins = wins + 1;
point = 0;
break;
}
if (scorenew == 7)
{
System.out.println ("\n you lose");
point = 0;
loss = loss + 1;
break;
}
} while (scorenew != point || scorenew != 7);
}
}
System.out.println ("\n number of wins = " + wins
+ " and number of loss = " + loss +
" and the probability for winning a game = " + (double) wins / (wins + loss));
}
public static int roll (int d1, int d2)
{
Random randomGenerator = new Random ();
int dice1 = randomGenerator.nextInt (6) + 1;
int dice2 = randomGenerator.nextInt (6) + 1;
System.out.println ("\n dice1 = " + dice1 + " dice2 = " + dice2);
int score = dice1 + dice2;
return score;
}
Everytime I run the code the do-while condition gets executed first, so please can anyone help me figure out where I am going wrong?
do {
} while(scorenew!=point || scorenew != 7);
This condition is always true, so you have an infinite loop. Also, why do you pass d1 and d2 into the roll() function? They are completely unused and unneeded.
Do while is doing exactly what it you should expect it to do. Executes the body first then evaluates the conditional to see if it should run again. You don't actually need a do while though, you want to run until one of the conditions breaks you out of the while loop.
else {
point = score;
System.out.println ("\n Point = " + point);
while (true) {
scorenew = roll (dice1, dice2);
System.out.println ("\n Score new = " + scorenew);
if (scorenew == point) {
System.out.println ("\n you win");
wins = wins + 1;
break;
}
if (scorenew == 7) {
System.out.println ("\n you lose");
loss = loss + 1;
break;
}
}
}
Like #Lee Daniel Crocker said you don't need to pass in dice1 and dice2 to the roll function.
public static int roll() {
Random randomGenerator = new Random();
int dice1 = randomGenerator.nextInt(6) + 1;
int dice2 = randomGenerator.nextInt(6) + 1;
System.out.println("\n dice1 = " + dice1 + " dice2 = " + dice2);
return dice1 + dice2;
}
Another thing that might help is not declaring all the variables at the top of your method. You don't need scorenew or point outside of the third condition, in fact you don't need scorenew at all since you have point:
public static void main(String[] args) throws java.lang.Exception {
int wins = 0;
int loss = 0;
for (int i = 0; i < 10000; i++) {
System.out.println("roll the dices");
int score = roll();
System.out.println("\n score " + score);
if (score == 7 || score == 11) {
System.out.println("\n Score = " + score);
System.out.println("you win");
wins = wins + 1;
} else if (score == 2 || score == 3 || score == 12) {
System.out.println("\n Score = " + score);
System.out.println("you lose");
loss = loss + 1;
} else {
int point = score;
System.out.println("\n Point = " + point);
while (true) {
score = roll();
System.out.println("\n Score new = " + score);
if (score == point) {
System.out.println("\n you win");
wins = wins + 1;
break;
}
if (score == 7) {
System.out.println("\n you lose");
loss = loss + 1;
break;
}
}
}
}
System.out.println("\n number of wins = " + wins
+ " and number of loss = " + loss +
" and the probability for winning a game = " + (double) wins / (wins + loss));
}
public static int roll() {
...
}

Need help adding "Do you want to play again" statement in my program

I'm making a Craps java program and I'm having some trouble adding a "Do you want to play again" statement at the end. If you can help me out that would be greatly appreciated! Also, the counter that I have to count how many games the user won/lost is not working properly. If you see the problem can you please tell me!
import java.util.Scanner;
public class Lab5 {
static int dice2;
public static void main(String[] args) {
//variables
int dice1;
int dice2;
int numWins = 0;
int numLosses = 0;
//Call the welcome method
welcome();
//fetch random numbers
/*
* **************************************************************
*welcome method
*welcome user
*no parameters
*no return
****************************************************************
*/
}
public static void welcome() {
System.out.println("Welcome to a Lucky (for me) Dice Game! \nFEELING LUCKY?!? Hope you brought lots of CASH!");{
}
int die1 = (int) (Math.random()*6 + 1);
int die2 = (int) (Math.random()*6 + 1);
int dice = die1 + die2;
System.out.println("Roll: total = " + dice);
int numWins = 0;
int numLosses = 0;
if (dice == 7 || dice == 11){
System.out.println("Woah!!! With a: "+dice+ " You WIN!!!!!!!!");
numWins++;
}
else if (dice == 2 || dice == 3 || dice == 12){
System.out.println("Sorry, with a "+dice+" You lose:(");
numLosses++;
}
while (dice != 0){
int die3 = (int) (Math.random()*6 + 1);
int die4 = (int) (Math.random()*6 + 1);
int dice2 = die3 + die4;
System.out.println("Roll: total = "+dice2);
if (dice2 == 2|| dice2 == 3 || dice2 == 12){
System.out.println("Sorry, with a "+dice2+" You lose:(");
numLosses++;
dice = 0;
}
else if (dice2 == 7 || dice2 == 11){
System.out.println("Woah!!! With a: "+dice2+ " You WIN!!!!!!!!");
numWins++;
dice = 0;
}
{
System.out.println("So far you have won " + numWins +
" times and lost " + numLosses + " times, ");
{
}
}
}
}}
This is my output when I run it:
Welcome to a Lucky (for me) Dice Game!
FEELING LUCKY?!? Hope you brought lots of CASH!
Roll: total = 2
Sorry, with a 2 You lose:(
Roll: total = 8
So far you have won 0 times and lost 1 times,
Roll: total = 10
So far you have won 0 times and lost 1 times,
Roll: total = 8
So far you have won 0 times and lost 1 times,
Roll: total = 3
Sorry, with a 3 You lose:(
So far you have won 0 times and lost 2 times,
The counter should only be stated after the win or lose. How do I fix this?
To repeat something, use a loop, such as a while loop or a do-while loop if you're not sure how manny times you'll loop. To get user input, use a Scanner object -- which you've already imported. The do-while loop structure will be something like...
do {
// code to do inside of the loop
} while (somethingIsTrue);
You will need to use some sentinel variable to change in the loop and then test inside of the while boolean check. It could be a String, in which case you'd use the String equals(...) or equalsIgnoreCase(...) method in your while's boolean check.
So consider prompting for input at the end of the do block of code using System.out.print(...), getting the input with your Scanner, and then testing that input in the while boolean test.
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Lab5
{
static int dice2;
public static void main(String[] args)
{
//variables
int dice1;
int dice2;
int numWins = 0;
int numLosses = 0;
//Call the welcome method
//welcome();
//fetch random numbers
/*
* **************************************************************
*welcome method
*welcome user
*no parameters
*no return
****************************************************************
*/
while(true)
{
welcome();
}
}
public static void welcome()
{
System.out.println("Welcome to a Lucky (for me) Dice Game! \nFEELING LUCKY?!? Hope you brought lots of CASH!");
int die1 = (int) (Math.random()*6 + 1);
int die2 = (int) (Math.random()*6 + 1);
int dice = die1 + die2;
System.out.println("Roll: total = " + dice);
int numWins = 0;
int numLosses = 0;
if (dice == 7 || dice == 11)
{
System.out.println("Woah!!! With a: "+dice+ " You WIN!!!!!!!!");
numWins++;
}
else if (dice == 2 || dice == 3 || dice == 12)
{
System.out.println("Sorry, with a "+dice+" You lose:(");
numLosses++;
}
while (dice != 0)
{
int die3 = (int) (Math.random()*6 + 1);
int die4 = (int) (Math.random()*6 + 1);
int dice2 = die3 + die4;
System.out.println("Roll: total = "+dice2);
if (dice2 == 2|| dice2 == 3 || dice2 == 12)
{
System.out.println("Sorry, with a "+dice2+" You lose:(");
numLosses++;
dice = 0;
}
else if (dice2 == 7 || dice2 == 11)
{
System.out.println("Woah!!! With a: "+dice2+ " You WIN!!!!!!!!");
numWins++;
dice = 0;
}
{
System.out.println("So far you have won " + numWins +
" times and lost " + numLosses + " times, ");
{
}
}
}
System.out.printf("\n\nWould you like to play again? ");
Scanner scanner = new Scanner(System.in);
String uinput = scanner.nextLine();
if(uinput.isEmpty() ||
uinput.equals("n") || uinput.equals("no"))
{
scanner.close();
System.out.println("Goodbye.");
return;
}
System.out.println();
}}

If statement layout not right

I am trying to create a program to assign cards a value and then depending which player gets the highest card then they win a point now the area which i cant get to work is the last if statement as it does it after every round not the 7 as needed.
import java.util.*;
public class Card {
public static void main(String[] args) {
int player1= 0;
int player2 = 0;
int i = 1;
while ( i <= 7) {
int player1Card = (int) (Math.random() * 13) + 1;
int player2Card = (int) (Math.random() * 13) + 1;
System.out.println("player 1 = " + player1Card);
System.out.println("player 2 = " + player2Card);
if (player1Card > player2Card) {
System.out.println("Player 1 wins!!!");
player1 = player1 + 1;
} else if (player1Card == player2Card){
System.out.println("It's a bore draw");
player1 = player1 + 0;
player2= player2 + 0;
} else {
System.out.println("Player 2 wins!!!!!");
player2 = player2 + 1;
}
System.out.println("Player 1 points " + player1);
System.out.println("Player 2 points " + player2);
i++;
if (player1 > player2) {
System.out.println("The winner is player 1 with " + player1 + " points");
} else if (player1 == player2) {
System.out.println("Its a draw");
} else {
System.out.println("The winner is Player 2 with " + player2 + " points");
}
}
Your if statement needs to come after the end of the while loop. Move the last } to be before the if statement you want to be executed after all 7 times have run.

Categories