Question

In: Computer Science

Why does the loop dont stop after Player1 wins. It stops after player2 wins but not...

Why does the loop dont stop after Player1 wins. It stops after player2 wins but not player1. After player1 wins it asks player2 to play again. Can someone pls help?
import java.util.Scanner;
public class Project2 {

public static void main(String [] args) {
Scanner input= new Scanner(System.in);
int total = 15, max_stones = 5, num_stones_removed;
int pile = total; //all stones are in the pile to start
int count = total;
boolean won= false;
boolean player2=false;
boolean player1=false;
  
while (pile>0 && won==false) {
  
while (player1==false){
System.out.println("Player1- How many stones you want to remove between 1 and " + max_stones);
num_stones_removed= input.nextInt();
System.out.print(num_stones_removed);
  
if (num_stones_removed>=1 && num_stones_removed<=max_stones && num_stones_removed<=pile) {
pile =pile-num_stones_removed; //update pile
player1=true;
count--;
System.out.println("You have " + pile + " stones left");
  
if (pile==0) {
won=true;
System.out.println("Play1 won");
break;
}
} else {
System.out.println("Invalid pick");
}// end else
}
player1=false;   
while (player2==false) {
System.out.println("Player2- How many stones you want to remove between 1 and " + max_stones);
num_stones_removed= input.nextInt();
  
if (num_stones_removed>=1 && num_stones_removed<=max_stones && num_stones_removed<=pile) {
  
pile =pile-num_stones_removed; //update pile
player2=true;
count--;
System.out.println("You have " + pile + " stones left");
  
if (pile==0) {
won=true;
System.out.println("Play2 won"); // player 2 wins
break;
}
}
  
else {
System.out.println("I am inside else-loop for P2"); //invalid pick for player2
System.out.println("Invalid pick");
}// end else
}
  
player2=false;

}// while loop
}}

  

Solutions

Expert Solution

Put player2=false; condition after while loop

Attached here the modified code:

import java.util.Scanner;
public class Project2 {

public static void main(String [] args) {
Scanner input= new Scanner(System.in);
int total = 15, max_stones = 5, num_stones_removed;
int pile = total; //all stones are in the pile to start
int count = total;
boolean won= false;
boolean player2=false;
boolean player1=false;
  
while (pile>0 && won==false) {
  
while (player1==false){
System.out.println("Player1- How many stones you want to remove between 1 and " + max_stones);
num_stones_removed= input.nextInt();
System.out.print(num_stones_removed);
  
if (num_stones_removed>=1 && num_stones_removed<=max_stones && num_stones_removed<=pile) {
pile =pile-num_stones_removed; //update pile
player1=true;
count--;
System.out.println("You have " + pile + " stones left");
  
if (pile==0) {
won=true;
System.out.println("Play1 won");
break;
}
} else {
System.out.println("Invalid pick");
}// end else
}
player1=false;   
while (player2==false) {
System.out.println("Player2- How many stones you want to remove between 1 and " + max_stones);
num_stones_removed= input.nextInt();
  
if (num_stones_removed>=1 && num_stones_removed<=max_stones && num_stones_removed<=pile) {
  
pile =pile-num_stones_removed; //update pile
player2=true;
count--;
System.out.println("You have " + pile + " stones left");
  
if (pile==0) {
won=true;
System.out.println("Play2 won"); // player 2 wins
break;
}
}
  
else {
System.out.println("I am inside else-loop for P2"); //invalid pick for player2
System.out.println("Invalid pick");
}// end else
}
  
}// while loop
player2=false; //put this after while loop
}}

Output:

plz upvote if this found helpful for you


Related Solutions

How does the immune system stop itself after the pathogen is cleared? How are B and...
How does the immune system stop itself after the pathogen is cleared? How are B and T cells made? How does the immune system allow helpful commensal bacteria to survive but not potentially pathogenic bacteria in the mucosal tissues? How do the parts of the immune system communicate with each other?
In the Forward Chaining algorithm, after the algorithm stops, prove that for those atoms that are...
In the Forward Chaining algorithm, after the algorithm stops, prove that for those atoms that are not assigned to true during the inference process, there exists a model in the KB in which the atom is true and there exists a model in the KB in which the atom is false.
During protein synthesis, does translation stop when the signal peptide is synthesized or does translation stop...
During protein synthesis, does translation stop when the signal peptide is synthesized or does translation stop when the SRP (signal receptor particles) bind to the signal peptides -- assuming it's being sent to the endomembrane system?
- Why is influence and adherence so difficult in health loop? - What role does gamification...
- Why is influence and adherence so difficult in health loop? - What role does gamification play in influencing people? - Do social networks help people change their behavior? - Most people stop using their fitness apps after a few months? Why? - Some people are very affected by their fitness apps and use them all the time for guidance. Why?
why does citric acid cycle stop when oxygen is removed? Explain! 2) A patient takes an...
why does citric acid cycle stop when oxygen is removed? Explain! 2) A patient takes an antibiotic that is identified to have toxic effects on some mechanorecsptors. Later, this patient begins to experience hearing loss. Explain the connection! 3) explain why, if someone shines a bright light in your eyes at night, it takes several minutes after the light goes away for you to regain your night vision.
What are the contents of the array after the for-loop in the following code?
(IN C)What are the contents of the array after the for-loop in the following code?int array[ SIZE ][ SIZE ] = { { 4, 5, 6, 7, 8 },{ 1, 2, 3, 4, 5 },{ 3, 6, 7, 8, 9 },{ 2, 3, 4, 5, 6 },{ 5, 6, 7, 8, 9 } };int i;int *ptr = array[ 0 ];for( i = 0; i < SIZE * SIZE; i++ ) {if( i % SIZE < 2 ) {*( ptr +...
describe why the loop of Henle is a loop, not a straight line include what substances...
describe why the loop of Henle is a loop, not a straight line include what substances can leave what sections and the effects that has on both water and salt reabsorption?
why does sea water make someone more dehydrated? explain how is the nephron loop related to...
why does sea water make someone more dehydrated? explain how is the nephron loop related to this.
a. What is a stop-payment order? b. How long are stop-payment order(s) effective? c. Does a...
a. What is a stop-payment order? b. How long are stop-payment order(s) effective? c. Does a bank have any liability when they properly dishonor a check for insufficient funds? Explain.
What does sales return and allowances include? Why does theseller entertain these complains after the...
What does sales return and allowances include? Why does the seller entertain these complains after the merchandise is sold?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT