write a java program for a computer game in which 21 match sticks are there,user
ID: 3626064 • Letter: W
Question
write a java program for a computer game in which 21 match sticks are there,user should start the game and at the end computer should always be the winner?
Explanation / Answer
import java.util.Scanner; public class game { public game() { } public static void main(String[] args) { int sticks=21; int use=0; boolean input = true; Scanner readIn = new Scanner(System.in); System.out.print("Player 1 Starts!"); while(sticks>0) { System.out.println("PLAYER 1 ' ' Enter Number of Sticks to pick up!(1-4)"); //Loop to ensure that the value entered is a numerical value that is positive while(input) { if(!readIn.hasNextInt()) { System.out.println("Please enter an integer value!!!"); System.out.print("Number of Sticks:$"); //Preps the statement for the next readIn.hasNextInt(), otherwise will give infinite loop readIn.next(); } else use=readIn.nextInt(); //Checks if positive if(use4||use>sticks) { System.out.println("Please enter a number between 1 and 4 and less then or equal to "+sticks); System.out.print("Number of Sticks:"); } else input=false; } input=true; System.out.println("Player 1 Took:"+use+" sticks"); sticks-=use; if(sticksRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.