私はJavaプログラミングの初心者で、じゃんけんゲームを作ろうとしていました。コメントが不足していることをお詫び申し上げます。
これは私のメインプログラムであり、他の2つのオブジェクトを呼び出します。
import java.util.*;
public class RPSMain extends RPSPlayer{
RPSPlayer player = new RPSPlayer();
RPSGame gameObject = new RPSGame ();
public void main () {
Random generator = new Random ();
Scanner sc = new Scanner(System.in);
System.out.print ("Number of Rounds: ");
int rounds = sc.nextInt();
//Call and process all of the methods found in RPSPlayer and RPSGame
for (int i = 0; i < rounds; i++){
player.makeThrow();
gameObject.makeThrow();
gameObject.announceWinner (compThrow, getPThrow);
}
//Final Output
System.out.print (gameObject.bigWinner(winner, rounds));
}
//accessor to return round to RPSGame
public static int getRound (int round){
this.round = round;
return round;
}
}
私の最初のオブジェクトは、プレーヤーが希望するスロー数を入力し、そこで処理される場所です。
import java.util.*;
public class RPSPlayer {
public static void main (String args[]) {
Random generator = new Random ();
Scanner sc = new Scanner(System.in);
}
//This method gets the throw, and loops if throw is not within 1 and 3
public static int makeThrow (){
Scanner sc = new Scanner (System.in);
int playerThrow;
do{
System.out.print ("Enter your throw (1=Rock, 2=Paper, 3=Scissors)");
playerThrow = sc.nextInt();
} while (playerThrow > 3 && playerThrow < 1);
return playerThrow;
}
//Accessor method
public static int getThrow (int playerThrow){
this.playerThrow = playerThrow;
return playerThrow;
}
}
最後のオブジェクトは、すべての計算が行われる場所です。正しくコンパイルされない変数がたくさんあり、その理由がよくわかりません...
import java.util.*;
public class RPSGame extends RPSPlayer{
RPSPlayer player = new RPSPlayer();
RPSGame game = new RPSGame ();
RPSMain mainRPS = new mainRPS();
public static void main (String args[]) {
Random generator = new Random ();
Scanner sc = new Scanner(System.in);
int rounds = mainRPS.getRound(rounds);
}
//Random Throw Generator
public static int makeCompThrow (){
int Max = 3;
int Min = 1;
int compThrow = Min + (int)(Math.random() * ((Max - Min) + 1));
return compThrow;
}
// Get the throw from the player in RPSPlayer
public static int getPlayerThrow (){
RPSPlayer player = new RPSPlayer();
int getPThrow = player.makeThrow();
return getPThrow;
}
//Does all of the calculatoins and ouputs who threw what.
public static int announceWinner (int compThrow, int getPThrow) {
int winner = 0;
if (getPThrow == 1){
System.out.println ("Player throws ROCK.");
}
else if (getPThrow == 2){
System.out.println ("Player throws PAPER.");
}
else if (getPThrow == 3){
System.out.println ("Player throws SCISSORS.");
}
if (compThrow == 1){
System.out.println ("Computer throws ROCK.");
}
else if (compThrow == 2){
System.out.println ("Computer throws PAPER.");
}
else if (compThrow == 3){
System.out.println ("Computer throws SCISSORS.");
}
if (getPThrow == compThrow){
winner = 3;
}
else if (getPThrow == 1 && compThrow == 3){
winner = 1;
}
else if (getPThrow == 1 && compThrow == 2){
winner = 2;
}
else if (getPThrow == 2 && compThrow == 1){
winner = 1;
}
else if (getPThrow == 2 && compThrow == 3){
winner = 2;
}
else if (getPThrow == 3 && compThrow == 1){
winner = 2;
}
else if (getPThrow == 3 && compThrow == 2){
winner = 1;
}
return winner;
}
//Final Output with imported values of 'rounds' and 'winner'
public int bigWinner (int winner, int rounds){
int tie = 0;
int playerWins = 0;
int compWins = 0;
if (winner == 1){
playerWins = playerWins + 1;
}
else if (winner == 0){
tie = tie + 1;
}
else if (winner == 3){
compWins = compWins + 1;
}
System.out.println ("You win " +playerWins+ ". Computer wins " +(compWins)+ ".");
if (playerWins > compWins){
System.out.print ("You win!");
}
if (playerWins < compWins){
System.out.print ("Computer wins!");
}
if (playerWins == compWins){
System.out.print ("It's a tie!");
}
return tie;
}
}
再度コンパイルすると、WATTO Studiosの提案を追加した後、2つの新しいエラーが表示されます。これらはコンパイラエラーです。
RPSMain.java:23: cannot find symbol
symbol : variable winner
location: class RPSMain
RPSGame.bigWinner(winner, rounds);
^
RPSMain.java:23: non-static method bigWinner(int,int) cannot be referenced
from a static context
RPSGame.bigWinner(winner, rounds);
RPSGameから参照している場合、変数「winner」が見つからないのはなぜですか。また、RPSMainで変数を検索しているのはなぜですか。