I am trying to run the while loop below for my Programming I class extra credit. It's supposed to simulate a game of craps however I can't figure out why the bottom while loop runs no matter what.
import java.util.Random;
import java.util.Scanner;
public class ExtraCredit {
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
Random random = new Random( );
int die1, die2, roll;
int dice = 0;
int point = 0;
int finish = 0;
int play = 1;
while (play == 1)
{
System.out.println ("Enter 0 to roll the dice");
roll = scan.nextInt( );
if (roll == 0)
{
die1 = random.nextInt( 6 ) + 1;
die2 = random.nextInt( 6 ) + 1;
dice = die1+die2;
}
if (dice == 7 || dice == 11)
{
System.out.println ("\nYou rolled a " + dice + " on your first roll!");
System.out.println ("You win!");
finish = 1;
}
else if (dice == 2 || dice == 3 || dice == 12)
{
System.out.println ("You rolled a " + dice + " on your first roll!");
System.out.println ("You lose!");
finish = 1;
}
else
{
point = dice;
System.out.println ("\nYour point is " + point);
System.out.println ("You need to roll a " + point + " to win");
System.out.println ("Enter 0 to Roll again");
roll = scan.nextInt( );
if (roll == 0)
{
die1 = random.nextInt( 6 ) + 1;
die2 = random.nextInt( 6 ) + 1;
dice = die1+die2;
}
}
while (dice != point || dice != 7 || finish != 1)
{
System.out.println ("\nYou rolled a " + dice);
System.out.println ("Roll again");
roll = scan.nextInt( );
if (roll == 0)
{
die1 = random.nextInt( 6 ) + 1;
die2 = random.nextInt( 6 ) + 1;
dice = die1+die2;
}
}
if (dice == point)
{
System.out.println ("\nYou rolled your point (" + point +")");
System.out.println ("You won!");
}
if (dice == 7)
{
System.out.println ("\nYou rolled a 7 before you could match your point");
System.out.println ("You Lose!");
}
System.out.println ("\nEnter 1 to play again");
play = scan.nextInt( );
}
}
}
(dice != point || dice != 7 || finish != 1)
This will never be false.
Try this
(dice != point && dice != 7 && finish != 1)
You want to change the ||'s to &&'s
while (dice != point && dice != 7 && finish != 1)
Related
I am coding a pig game in Java, and I need help adding up the score. The game's goal is to roll two dice, and the values of those two dice are added together, and the player who gets to 100 first wins. I want to loop the values (which I called "Added") so that it continuously adds up by themselves.
Thank you in advance
By the way, I'm hardly done most of the game, so mind the gaps, lol.
import java.util.Random;
import java.util.Scanner;
public class Pig {
static int player, Continue, roll1, roll2;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
Random r2 = new Random();
System.out.print("Enter 1 to play against computer\nEnter 2 to play against another person\n");
player = keyboard.nextInt();
if (player == 1) {
System.out.println("\nPlaying against the computer...");
roll1 = r.nextInt(6) + 1;
System.out.println("\nYour first roll is a " + roll1);
roll2 = r.nextInt(6) + 1;
System.out.println("\nYour second roll is a " + roll2);
int added = roll1 + roll2;
System.out.println("\nYour total is " + added);
if ((roll1 == 1) || (roll2 == 1)) {
System.out.println("\nYou rolled a 1, you lose all your points!");
}
System.out.print("\nEnter 1 to continue rolling\nEnter 2 to give up the dice\n");
Continue = keyboard.nextInt();
if (Continue == 1) {
do {
roll1 = r.nextInt(6) + 1;
System.out.println("\nYour first roll is a " + roll1);
roll2 = r.nextInt(6) + 1;
System.out.println("\nYour second roll is a " + roll2);
int added2 = roll1 + roll2;
int added3 = added + added2;
added = added2;
added2 = added3;
System.out.println("\nYour total is " + added3);
if ((roll1 == 1) || (roll2 == 1)) {
System.out.println("\nYou rolled a 1, you lose all your points!");
}
System.out.print("\nEnter 1 to continue rolling\nEnter 2 to give up the dice\n");
Continue = keyboard.nextInt();
} while (Continue == 1);
}
} else {
System.out.print("Bye");
}
/*if (player == 2){
System.out.println("Playing against another person...");
}
else {
System.out.print("Bye");
count++;
*/
}
}
Ok, so I have created the following program. It is not yet complete but when the code comes to the end (y or n part) and the user decides to try again they don't get the option to enter a new bet it just uses the one entered from the first time.
(Please comment if you need help about understanding the code or thinking it might be hard for other people to understand)
import java.util.*;
public class Lab15 {
static Scanner kin = new Scanner(System.in);
public static void main(String[] args) {
double money = 1000;
System.out.println("Welcom To Craps You Start With 1000$!!!");
int play = 1; //variable to cancel whole loop
char yesNo; //if user wants to continue playing or not
while(money > 0 && play == 1) {
System.out.print("Please Enter The Amount You Want To Bet: ");
double bet = kin.nextDouble();
while((bet <= money || bet > 0)) {
do { //loop to test if the bet is legit
if(money < bet || bet < 0) {
System.out.print("Sorry Invalid Bet; Please Enter A Legitimant Bet: ");
bet = kin.nextDouble();
}
} while(bet > money || bet < 0);
money -= bet;
System.out.println("You Have Bet $" + bet + " And Have " + money + " Left");
int die1 = (1 + (int) (6 * Math.random()));
int die2 = (1 + (int) (3 * Math.random()));
int sum = die1 + die2;
System.out.println("You Rolled " + die1 + " and " + die2 + " totaling " + sum);
System.out.print("Do You Want To Play Again(y) Or Not(n)");
yesNo = kin.next().charAt(0);
int loopBreak = 0; //to break do while loop bellow
do {
if(yesNo == 'y') { //take above yes no to restart loop or not
System.out.println("Restarting...");
loopBreak += 1;
continue;
}
else if(yesNo == 'n') {
System.out.println("Thank You For Playing");
play -= 1; //to cancel whole program
break;
}
else {
System.out.println("Please Enter 'y' Or 'n'");
}
} while(loopBreak == 0);
if(play == 0) {
break;
}
}
}
}
}
Its all about nested while loop don't stop when it needed. Its like this
while(money > 0 && play == 1) {
System.out.print("Please Enter The Amount You Want To Bet: ");
double bet = kin.nextDouble();
while((bet <= money || bet > 0)) {
...
do {
if(yesNo == 'y') { //take above yes no to restart loop or not
System.out.println("Restarting...");
loopBreak += 1;
continue;
}
else if(yesNo == 'n') {
System.out.println("Thank You For Playing");
play -= 1; //to cancel whole program
break;
}
else {
System.out.println("Please Enter 'y' Or 'n'");
}
} while(loopBreak == 0);
if(play == 0) {
break;
}
}
}
After you get answer 'y' you don't break the nested while and program never get the
while(money > 0 && play == 1) {
--> System.out.print("Please Enter The Amount You Want To Bet: ");
--> double bet = kin.nextDouble();
//because below while loop continues to loop
while((bet <= money || bet > 0)) {
...
}
}
part. Anyways corrected code is here(i tried every possibility and it worked)
import java.util.*;
public class Lab15 {
static Scanner kin = new Scanner(System.in);
public static void main(String[] args) {
double money = 1000;
System.out.println("Welcom To Craps You Start With 1000$!!!");
int play = 1; //variable to cancel whole loop
char yesNo; //if user wants to continue playing or not
while(money > 0 && play == 1) {
System.out.print("Please Enter The Amount You Want To Bet: ");
double bet = kin.nextDouble();
while((bet <= money || bet > 0)) {
do { //loop to test if the bet is legit
if(money < bet || bet < 0) {
System.out.print("Sorry Invalid Bet; Please Enter A Legitimant Bet: ");
bet = kin.nextDouble();
}
} while(bet > money || bet < 0);
money -= bet;
System.out.println("You Have Bet $" + bet + " And Have " + money + " Left");
int die1 = (1 + (int) (6 * Math.random()));
int die2 = (1 + (int) (3 * Math.random()));
int sum = die1 + die2;
System.out.println("You Rolled " + die1 + " and " + die2 + " totaling " + sum);
System.out.print("Do You Want To Play Again(y) Or Not(n): ");
yesNo = kin.next().charAt(0);
int loopBreak = 0; //to break do while loop bellow
do {
if(yesNo == 'y') { //take above yes no to restart loop or not
System.out.println("Restarting...");
loopBreak++;
continue;
}
else if(yesNo == 'n') {
System.out.println("Thank You For Playing");
play--; //to cancel whole program
break;
}
else {
System.out.println("Please Enter 'y' Or 'n': ");
}
} while(loopBreak == 0);
if(play == 0 || loopBreak == 1) {
break;
}
}
}
}
}
Have a good day!
Another solution is to use labeled breaks/continues https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
However it should be used sparingly (or not at all). You probably can simplify it in some other way.
package test.test;
import java.util.Scanner;
public class Lab15 {
static Scanner kin = new Scanner(System.in);
public static void main(String[] args) {
double money = 1000;
System.out.println("Welcom To Craps You Start With 1000$!!!");
int play = 1; // variable to cancel whole loop
char yesNo; // if user wants to continue playing or not
//Labeled <-----------------
start: while (money > 0 && play == 1) {
System.out.print("Please Enter The Amount You Want To Bet: ");
double bet = kin.nextDouble();
while ((bet <= money || bet > 0)) {
do { // loop to test if the bet is legit
if (money < bet || bet < 0) {
System.out.print("Sorry Invalid Bet; Please Enter A Legitimant Bet: ");
bet = kin.nextDouble();
}
} while (bet > money || bet < 0);
money -= bet;
System.out.println("You Have Bet $" + bet + " And Have " + money + " Left");
int die1 = (1 + (int) (6 * Math.random()));
int die2 = (1 + (int) (3 * Math.random()));
int sum = die1 + die2;
System.out.println("You Rolled " + die1 + " and " + die2 + " totaling " + sum);
System.out.print("Do You Want To Play Again(y) Or Not(n)");
yesNo = kin.next().charAt(0);
int loopBreak = 0; // to break do while loop bellow
do {
if (yesNo == 'y') { // take above yes no to restart loop or not
System.out.println("Restarting...");
loopBreak += 1;
//Labeled continue <-----------------
continue start;
} else if (yesNo == 'n') {
System.out.println("Thank You For Playing");
play -= 1; // to cancel whole program
break;
} else {
System.out.println("Please Enter 'y' Or 'n'");
}
} while (loopBreak == 0);
if (play == 0) {
break;
}
}
}
}
}
So, I'm working on this assignment for a class. It's a Java class, and I'm supposed to make a game where it rolls two dice, adds them up, and adds them to your turn score. It then asks if you want to keep playing. When your turn score hits 20, or when you decide to pass, it goes to a computer. It's supposed to print the scores each turn, and then when someone hits 100 points, it announces the winner. However, no matter what, the score at the end of each turn is 0, no matter how many times I run it. When a player rolls a 1, their turn score is cancelled, and it moves on to the other player, and if they roll double 1's, they lose all of their points for the game so far. Here is my code, can you figure out why the score variables don't update? Thank you.
import java.util.Scanner;
import java.util.Random;
public class PlayPig {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int player1 = 0;
int player2 = 0;
int a, b, c, player1turn, player2turn, input;
int pig = 1;
Random r = new Random();
do{
do {
player1turn=0;
a = r.nextInt(6)+1;
b = r.nextInt(6)+1;
if( a==1 || b==1){
if (a == 1 && b == 1){
player1=0;
break;}
else if (a==1 || b==1){
player1turn=0;
break;}
else {
player1turn= a+b ;
}}
player1= player1+player1turn;
System.out.println("Player1 score is " + player1 + " and player2 score is " + player2);
System.out.print("Do you want to keep playing? Enter 1 to continue. Enter any other number to pass.");
input = scan.nextInt();
if (input != 1)
break;
}
while
(player1turn <= 20);
do{
player2turn=0;
a = r.nextInt(6)+1;
b = r.nextInt(6)+1;
if( a==1 || b==1){
if (a == 1 && b == 1){
player2=0;
break;}
else if (a==1 || b==1){
player2turn=0;
break;}
else {
player1turn= a+b ;
player2= player2+player2turn;}}
}
while
(player2turn<=20);
}
while
(player1 < 100 || player2 < 100);
if (player1>player2)
System.out.print("Player 1 wins");
else
System.out.print("Player 2 wins");
}}
The main problem was, that your else conditions in which you assigned the current score were in the wrong block. (These ones):
else {
player1turn = a+b ;
}
Try this code:
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int player1 = 0;
int player2 = 0;
int a, b, c, player1turn, player2turn, input;
int pig = 1;
Random r = new Random();
do{
do {
player1turn=0;
a = r.nextInt(6)+1;
b = r.nextInt(6)+1;
if( a==1 || b==1){
if (a == 1 && b == 1){
player1 = 0;
break;
}
else if (a==1 || b==1){
player1turn=0;
break;
}
}else {
player1turn = a+b ;
}
player1 += player1turn;
System.out.println("Player1 score is " + player1 + " and player2 score is " + player2);
System.out.print("Do you want to keep playing? Enter 1 to continue. Enter any other number to pass.");
input = scan.nextInt();
if (input != 1){
break;
}
} while (player1turn <= 20);
do{
player2turn=0;
a = r.nextInt(6)+1;
b = r.nextInt(6)+1;
if( a==1 || b==1){
if (a == 1 && b == 1){
player2=0;
break;
} else if (a==1 || b==1){
player2turn=0;
break;
}
}else {
player2turn = a+b ;
player2 += player2turn;
}
}while (player2turn<=20);
} while (player1 < 100 || player2 < 100);
if (player1>player2)
System.out.print("Player 1 wins");
else
System.out.print("Player 2 wins");
}
I have modified the if loop.
You can try this:
import java.util.Scanner;
import java.util.Random;
public class PlayPig {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int player1 = 0;
int player2 = 0;
int a, b, c, player1turn, player2turn, input;
int pig = 1;
Random r = new Random();
do{
do {
player1turn=0;
a = r.nextInt(6)+1;
b = r.nextInt(6)+1;
if (a == 1 && b == 1){
player1=0;
break;
}
else if((a== 1 && b!= 1) || (a!=1 && b== 1){
player1turn=0;
break;
}
else{
player1turn= a+b ;
}
player1= player1+player1turn;
System.out.println("Player1 score is " + player1 + " and player2 score is " + player2);
System.out.print("Do you want to keep playing? Enter 1 to continue. Enter any other number to pass.");
input = scan.nextInt();
if (input != 1)
break;
}
while
(player1turn <= 20);
do{
player2turn=0;
a = r.nextInt(6)+1;
b = r.nextInt(6)+1;
if (a == 1 && b == 1){
player2=0;
break;
}
else if((a== 1 && b!= 1) || (a!=1 && b== 1){
player2turn=0;
break;
}
else{
player2turn= a+b ;
}
player2= player2+player2turn;
}
while
(player2turn<=20);
}
while
(player1 < 100 || player2 < 100);
if (player1>player2)
System.out.print("Player 1 wins");
else
System.out.print("Player 2 wins");
}}
I can't make this program show the message "Number out of range" when the user put a number out of (1 - 6). I tried different ways of code, but I don't know what I am doing wrong.
public static void main(String[] args) {
int guess = 0;
int tries = 3;
Scanner input = new Scanner(System.in);
Random ran = new Random();
int diceNumber = ran.nextInt(6)+1;
System.out.println(diceNumber);
System.out.println("Insert a number between 1 and 6 : you have " + tries + " tries.");
guess = input.nextInt();
while((tries >1 && (guess <7 || guess>0))) {
tries--;
System.out.println("Incorrect Number, you have " +tries+ " more tries.");
guess = input.nextInt();
}if(guess >=7 || guess<=0 && (tries >1)){
--tries;
System.out.println("Number out of range, try again; you have " + tries + " more tries.");
guess = input.nextInt();
} if(tries ==0 || guess != diceNumber) {
System.out.println("You Lose!!");
} else if(guess == diceNumber){
System.out.println("You Win!!");
}
}
You need to position the condition to check if the input is a valid input or not at the start of the while loop , currently, it will only be checked after all tries are exhausted as you end the while loop before it.
Also, you should change the while loop condition from (tries >1 && (guess <7 || guess>0)) to (tries > 1 && guess != diceNumber) , since you may not want to exit the loop if the number entered is outside the range.
Code -
public static void main(String[] args) {
int guess = 0;
int tries = 3;
Scanner input = new Scanner(System.in);
Random ran = new Random();
int diceNumber = ran.nextInt(6)+1;
System.out.println(diceNumber);
System.out.println("Insert a number between 1 and 6 : you have " + tries + " tries.");
guess = input.nextInt();
while((tries >1 && guess != diceNumber)) {
if((guess >=7 || guess<=0) && (tries >1)){
--tries;
System.out.println("Number out of range, try again; you have " + tries + " more tries.");
guess = input.nextInt();
}
else {
tries--;
System.out.println("Incorrect Number, you have " +tries+ " more tries.");
guess = input.nextInt();
}
}
if(tries ==0 || guess != diceNumber) {
System.out.println("You Lose!!");
} else if(guess == diceNumber){
System.out.println("You Win!!");
}
}
The below code will fetch you the expected result.
import java.util.Random;
import java.util.Scanner;
public class Dice {
public static void main(String[] args) {
int guess = 0;
int tries = 3;
Scanner input = new Scanner(System.in);
Random ran = new Random();
int diceNumber = ran.nextInt(6) + 1;
System.out.println(diceNumber);
System.out.println("Insert a number between 1 and 6 : you have "
+ tries + " tries.");
guess = input.nextInt();
loop: while ((tries >= 1)) {
--tries;
if ((guess >= 7 || guess <= 0) && (tries >= 1)) {
System.out.println("Number out of range, try again; you have "
+ tries + " more tries.");
guess = input.nextInt();
} else {
if (guess == diceNumber) {
System.out.println("You Win!!");
break loop;
} else if (tries >= 1) {
System.out.println("Incorrect Number, you have " + tries
+ " more tries.");
guess = input.nextInt();
}
}
}
if (tries == 0 && guess != diceNumber) {
System.out.println("You Lose!!");
}
}
}
I think the problem is in your while loop.
I'd say it should be something like this:
while(tries > 0 && guess != diceNumber) {
tries--;
if (guess >= 1 && guess <= 6) {
System.out.println("Incorrect Number, you have " +tries+ " more tries.");
} else if (guess >=7 || guess<=0) {
System.out.println("Number out of range, try again; you have " + tries + " more tries.");
}
if (tries > 0) {
guess = input.nextInt();
}
}
if(tries == 0 || guess != diceNumber) {
System.out.println("You Lose!!");
} else if(guess == diceNumber){
System.out.println("You Win!!");
}
I think this should do what you want.
As you can see, I changed the while condition:
You want the user to be able to enter another number while he has at least 1 try left, so tries>0.
You want the user to enter another number only if guess != diceNumber.
The if conditions inside also change:
You only say it's an incorrect number if the entered number is between 1 and 6: guess >= 1 && guess <= 6.
You only say it's out of range if the number is not between 1 and 6: guess >= 7 || guess <= 0.
You don't need to check the number of tries, the while loop does that already.
You only want to get a new input number if the user as a try left: tries > 0.
So this is my code now:
public class PigTry2
{
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
// Variables
int start;
int total;
//Methods
Scanner quest = new Scanner (System.in);
die1 x = new die1 ();
die2 z = new die2 ();
// Game
System.out.println("Hello. Would you like to play PIG? 1 for yes");
start = quest.nextInt();
if (start == 1){
x.roll();
z.roll();
System.out.println("You roll: "+ z.getEyes() + " " + x.getEyes());
do {
System.out.println("Would you like to roll again");
start = quest.nextInt();
if (start == 1)
x.roll();
z.roll();
System.out.println("You roll: "+ z.getEyes() + " " + x.getEyes());
} while(z.getEyes() != 1 && x.getEyes() != 1);
total =
}
}
}
I have tried a few different methods, and I want to add up the total sum of the rolls that happen. I simply can't figure out how to do this. Anyone able to help me out?
If I understand what you want, I'd suggest this:
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
// Variables
int start;
int total = 0;
//Methods
Scanner quest = new Scanner (System.in);
die1 x = new die1 ();
die2 z = new die2 ();
// Game
System.out.println("Hello. Would you like to play PIG? 1 for yes");
start = quest.nextInt();
if (start == 1){
x.roll();
z.roll();
total += 2; //You just rolled twice, so lets increment by 2
System.out.println("You roll: "+ z.getEyes() + " " + x.getEyes());
do {
System.out.println("Would you like to roll again");
start = quest.nextInt();
if (start == 1)
x.roll();
z.roll();
total += 2; //EDIT: Rolled again, so ingrement by 2 again
System.out.println("You roll: "+ z.getEyes() + " " + x.getEyes());
} while(z.getEyes() != 1 && x.getEyes() != 1);
System.out.println("Total amount of rolls: " + total);
}
}
Something else: Users won't be able to cancel rolling until they throw a "1", because the if-statement in the do-while-loop won't quit the loop. You could use a flag to indicate, whether the user want's to keep rolling:
boolean finished = false;
do {
System.out.println("Would you like to roll again");
start = quest.nextInt();
if (start == 1) {
x.roll();
z.roll();
total += 2; //EDIT: Rolled again, so ingrement by 2 again
System.out.println("You roll: "+ z.getEyes() + " " + x.getEyes());
} else {
finished = true;
}
} while(z.getEyes() != 1 && x.getEyes() != 1 && !finished);