Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please need help to correct this below Java code to compile correct, so the user can input number of sticks at the console. Thanks! import
Please need help to correct this below Java code to compile correct, so the user can input number of sticks at the console. Thanks! import java.util.Scanner;
public abstract class Main
protected String name;
public MainString name
this.name name;
public abstract int makeMoveint currentSticks;
class Computer extends Main
public Computer
superComputer;
@Override
public int makeMoveint currentSticks
Simple strategy: take a random valid amount to half of the current sticks
int max currentSticks ;
int move intMathrandommax ; Random number between and max
System.out.printlnname takes move sticks;
return move;
import java.util.Scanner;
class Human extends Main
private Scanner scanner;
public Human
superHuman;
scanner new ScannerSystemin;
@Override
public int makeMoveint currentSticks
int move ;
do
System.out.printlnEnter the number of sticks to take currentSticks : ;
while scanner.hasNextInt
System.out.printlnThats not a valid number!";
scanner.next; move on to the next input
move scanner.nextInt;
while move move currentSticks ;
return move;
class Game
private Main players new Main;
private int sticks;
public Gameint sticks
this.sticks sticks;
players new Computer;
players new Human;
public void start
int currentPlayer ;
while sticks
System.out.printlnCurrent sticks: sticks;
int move playerscurrentPlayermakeMovesticks;
sticks move;
if sticks
System.out.printlnplayerscurrentPlayername wins!";
break;
currentPlayer currentPlayer ; Switch players
public static void mainString args
if argslength
System.out.printlnPlease specify the initial number of sticks as a commandline argument.";
return;
int sticks Integer.parseIntargs;
Game game new Gamesticks;
game.start;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started