My GameDemo is supposed to play the game programs but I need the method play add
ID: 3642411 • Letter: M
Question
My GameDemo is supposed to play the game programs but I need the method play added into my game program, DiceGame.Eclipse error: The method play() is undefined for type DiceGame
GameDemo:
import java.util.Scanner;
public class GameDemo {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int GameChoice = 0;
DiceGame Game1 = new DiceGame();
DiceGame2 Game2 = new DiceGame2();
DiceGame3 Game3 = new DiceGame3();
DiceGame4 Game4 = new DiceGame4();
RandomGuess Game5 = new RandomGuess();
SkillGuess Game6 = new SkillGuess();
while(GameChoice != 7) {
menu();
GameChoice = input();
switch(GameChoice) {
case 1:
Game1.play(); //Here is where I am getting the error
break;
case 2:
Game2.play();
break;
case 3:
Game3.play();
break;
case 4:
Game4.play();
break;
case 5:
Game5.play();
break;
case 6:
Game6.play();
break;
case 7:
System.out.println("Closing the demo.");
break;
default:
break;
}
}
}
public static int input() {
int gameChoice = 0;
while(true) {
gameChoice = scan.nextInt();
if(gameChoice == 0)
menu();
if(gameChoice > 0 && gameChoice <= 7)
break;
else
System.out.println("Try again, 1 through 7 only. 0 for the menu again.");
}
return gameChoice;
}
public static void menu() {
System.out.println("1) Throw a particular die number a certain number of times in a row.");
System.out.println("2) Build a chart displaying the randomness of the 6 die numbers.");
System.out.println("3) Throw a given pattern of six dice.");
System.out.println("4) Find the percent difference for each die number from the average, given a certain number of throws.");
System.out.println("5) Guess the number between 1 and 100");
System.out.println("6) Guess a five digit number.");
System.out.println("7) Quit playing.");
System.out.println("Please choose one of the 7 choices.");
}
}
DiceGame:
import java.util.Scanner;
public class DiceGame
{
public int dieFaceNumber;
public int numberOfRow;
public long numberOfTimes;
public DiceGame()
{
input();
process();
output();
}
public static void main(String [] args)
{
DiceGame Game1 = new DiceGame();
}
public void input()
{
Scanner getInput = new Scanner(System.in);
System.out.println("This game throws a die until a certain dice face number appears in a row a certain number of times.");
System.out.println("Please enter the dice face number that you would like to be repeated (1-6):");
dieFaceNumber = getInput.nextInt();
System.out.println("Please enter the number of times you would like " + dieFaceNumber + " to appear in a row:");
numberOfRow = getInput.nextInt();
}
public void process()
{
int num;
int count = 0;
//long seed = 19580427;
//Random generator = new Random( seed );
numberOfTimes = 0;
while(count<numberOfRow)
{
numberOfTimes ++;
num = (int)((Math.random()*6) +1);
if (num == dieFaceNumber )
{
count ++;
}else
{
count = 0;
}
}
}
public void output()
{
System.out.println("It took " + numberOfTimes + " throws to get the number " + dieFaceNumber + " to appear " + numberOfRow + " in a row.");
System.out.println("Would you like to play the game again?");
System.out.println("Please enter (yes/no)");
Scanner getInput = new Scanner(System.in);
String choice= getInput.next();
if (choice.equals("yes")||choice.equals("y"))
{
new DiceGame();
}
}
}
I'm having the same error for all of the games, but I think if it is fixed in one I'll probably understand how to do it in all of them.
Explanation / Answer
So your switch block will become:
switch(GameChoice) {
case 1:
Game1.main(null);
break;
case 2:
Game2.main(null);
break;
case 3:
Game3.main(null);
break;
case 4:
Game4.main(null);
break;
case 5:
Game5.main(null);
break;
case 6:
Game6.main(null);
break;
case 7:
System.out.println("Closing the demo.");
break;
default:
break;
}
}
}
Please rate one answer lifesaver, and the other, whatever you like.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.