Question
Hi I am writing a Java program for a ThreeDice Game and I was wondering how to avoid this non-static method cannot be referenced from
Hi I am writing a Java program for a ThreeDice Game and I was wondering how to avoid this non-static method cannot be referenced from a static context compilation error.
Error from compiler is:
" java:21: error: non-static method NumberOfRoundsToPlay() cannot be referenced from a static context
NumberOfRoundsToPlay();
^
Game.java:24: error: non-static method StartRounds() cannot be referenced from a static context
StartRounds();"
NumberOfRoundsToPlay();
^
Game.java:24: error: non-static method StartRounds() cannot be referenced from a static context
StartRounds();
My question basically is how can I fix this code to make it compile. Thanks
"
import java.util.Scanner;
public class Game {
// Declaring Class Local Varibles private int Player1; private int Player2; private int Player1Total; private int Player2Total; private int dice1; private int dice2; private int dice3; private int RoundCounter; private int RoundsToPlay;
public static int main (String args[]) {
NumberOfRoundsToPlay();
StartRounds();
}
// This Method Gets User Input on How Many Games They Would Like to Play public void NumberOfRoundsToPlay() { try { System.out.print("How Many Games You Would Like to Play!"); Scanner in = new Scanner (System.in); RoundsToPlay = in.nextInt(); } catch (Exception e) { System.out.print("Please Enter a Positive Integer");}
NumberOfRoundsToPlay(); //Recursive to Prompt User for Valid Input
}
public void StartRounds() {
for (int i=1;i<=RoundsToPlay;i++) { PlayGame(); }
}
public void PlayGame() { RoundCounter = RoundCounter + 1; // Counts the number of rounds
// Player 1 Dice Roll dice1 = (int)(Math.random()*6+1); dice2 = (int)(Math.random()*6+1); dice3 = (int)(Math.random()*6+1);
System.out.print("Round " + RoundCounter + " Player 1: "); ThreeDice diceRollPlayer1 = new ThreeDice(dice1, dice2, dice3);
ThreeDiceScorer GetPoints = new ThreeDiceScorer(dice1, dice2,dice3);
diceRollPlayer1.printResult();
Player1 = Player1 + GetPoints.points(dice1,dice2,dice3); Player1Total = Player1Total + Player1; System.out.println("Player 1 Points: " + Player1);
// Player2 Dice Roll dice1 = (int)(Math.random()*6+1); dice2 = (int)(Math.random()*6+1); dice3 = (int)(Math.random()*6+1);
System.out.print("Round " + RoundCounter + " Player 2: "); ThreeDice diceRollPlayer2 = new ThreeDice(dice1, dice2, dice3);
diceRollPlayer2.printResult();
Player2 = Player2 + GetPoints.points(dice1,dice2,dice3); Player2Total = Player2Total + Player2; System.out.println("Player 2 Points: " + Player2);
if (Player1 < Player2) { System.out.println("Player2 is the Winner!");
} else if(Player1 == Player2) { System.out.println("It's a Tie!");
}
else if(Player1 > Player2) { System.out.println("Player1 is the Winner!");
}
System.out.println("");
}
}
"
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