【janken.RandomTacitcs cannot be resolved to a type】Javaのエラー
解決済
回答 2
投稿
- 評価
- クリップ 0
- VIEW 2,575

退会済みユーザー
janken.RandomTacitcs cannot be resolved to a type
とエラーが出ます。
どのように修正すればいいでしょうか?
宜しくお願いします
package Ovject;
public class janken {
// インターフェースの作成
public interface Tactis {
public int readTactis(); // メソッドの作成
}
// ランダムに手を決める戦略クラス.インタフェースをじっそうする
public class Player {
public int stone = 0;
int scissors = 1;
int paper = 2;
private String name_;
private int Wincount_ = 0;
private Tactis tactis_;
// コンストラクタ
Player(String name_) {
this.name_ = name_;
}
void SetTactics(Tactis tactis) {
tactis_ = tactis;
}
// showHandを実行するのではなく、readTactisを実行するようにする
int showHand() {
int hand = tactis_.readTactis();
return hand;
}
public class RandomTacitcs implements Tactis {
public int readTactis() {
double num = Math.random() * 3;
int hand = 0;
if (num < 1) {
hand = stone;
System.out.println("ぐー");
} else if (num < 2) {
hand = scissors;
System.out.println("ちょき");
} else {
hand = paper;
System.out.println("パー");
}
return hand;
}
}
// メソッド
public void notifyResult(boolean result) {
if (result = true) {
Wincount_++;
}
}
public String getName() {
return name_;
}
public int getWincount() {
return Wincount_;
}
}
public class Judge {
// メソッド
public void StartJunken(Player player1, Player player2) {
System.out.println("ジャンケンを開始します");
for (int i = 0; i < 3; i++) {
System.out.println(i + "回戦");
Player winner = judgeJanken(player1, player2);
if (winner != null) {
System.out.println(winner.getName() + "が勝ちました");
} else {
System.out.println("引き分けです");
}
}
System.out.println("じゃんけんを終了しました!");
Player finalWinner = judgeFinalWinner(player1, player2);
if (finalWinner != null) {
System.out.println(finalWinner.getName() + "の勝ちです!");
} else {
System.out.println("引き分けです!");
}
}
private Player judgeJanken(Player player1, Player player2) {
Player winner = null;
int playerhand1 = player1.showHand();
int playerhand2 = player2.showHand();
if (playerhand1 == player1.stone && playerhand2 == player2.scissors
|| playerhand1 == player1.scissors
&& playerhand2 == player2.paper
|| playerhand1 == player1.paper
&& playerhand2 == player2.stone) {
winner = player1;
player1.Wincount_++;
} else if (playerhand2 == player1.stone
&& playerhand1 == player2.scissors
|| playerhand2 == player1.scissors
&& playerhand1 == player2.paper
|| playerhand2 == player1.paper
&& playerhand1 == player2.stone) {
winner = player2;
player2.Wincount_++;
}
return winner;
}
private Player judgeFinalWinner(Player player1, Player player2) {
Player wineer = null;
int player1Wincounr = player1.getWincount();
int player2Wincounr = player2.getWincount();
if (player1Wincounr > player2Wincounr) {
wineer = player1;
} else if (player1Wincounr < player2Wincounr) {
wineer = player2;
} else {
return null;
}
return wineer;
}
}
public class Yamada extends Player {
public Yamada(String name_) {
super(name_);
}
public int showHand() {
System.out.println("ぐー");
return stone;
}
}
public class Murata extends Player {
public Murata(String name_) {
super(name_);
}
public int showHand() {
System.out.println("ぱー");
return paper;
}
}
public static void main(String[] args) {
janken j = new janken();
Judge saito = j.new Judge();
Player murata = j.new Murata("村田さん");
Player yamada = j.new Yamada("山田さん");
///エラー部分
Tactis murataTactis = j.new RandomTacitcs();
murata.SetTactics(murataTactis);
saito.StartJunken(murata, yamada);
}
}
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
参考になれば幸いです。
主な変更点;
- inner class は使わないようにした。
- じゃんけんの手を enum にした。
- じゃんけんの手の勝敗を if 文での判定でなく、勝敗表を引いて判定するようにした。
- Murata, Yamada というクラスは無くして、 Playser クラスで名前を持つようにした。
package Ovject;
import java.util.HashMap;
enum Hand {
STONE("グー"), SCISSORS("チョキ"), PAPER("パー");
private final String name_;
private Hand(String name) {
this.name_ = name;
}
public String getName() {
return this.name_;
}
}
// 戦術インターフェースの定義
interface Tactics {
public Hand nextHand();
}
// ランダムに手を選ぶ戦術
class RandomTactics implements Tactics {
private final Hand[] hands = {Hand.STONE, Hand.SCISSORS, Hand.PAPER};
@Override
public Hand nextHand() {
double num = Math.random() * 3;
return hands[(int) num];
}
}
class Player {
private final String name_; // 名前
private Tactics tactics_; // 戦術
private final HashMap<String, Integer> records_ = new HashMap<>(); // スコア
Player(String name) {
this(name, null);
}
Player(String name, Tactics tactics) {
this.name_ = name; // 名前
this.tactics_ = tactics; // 戦術
this.records_.put("win", 0); // 勝ちの回数
this.records_.put("lose", 0); // 負けの回数
this.records_.put("draw", 0); // 引分の回数
}
Hand nextHand() {
if (this.tactics_ != null) {
return this.tactics_.nextHand();
} else {
// デフォルトの戦術: 常に "グー" を出す
return Hand.STONE;
}
}
public void notifyResult(int result) {
final String[] keys = {"draw", "win", "lose"};
String key = keys[result + 1];
this.records_.put(key, 1 + this.records_.get(key));
}
public String getName() {
return name_;
}
public int score(String key) {
return this.records_.get(key);
}
String scoreStr() {
return this.getName()
+ " 勝ち:" + this.records_.get("win")
+ " 負け:" + this.records_.get("lose")
+ " 引分:" + this.records_.get("draw");
}
}
class Judge {
// じゃんけんの勝敗表 ["player1 の手 player2 の手", 勝敗"]
// 勝敗項の値: 0: 引分, 1: player1 の勝ち, -1: player1 の負け
private static final HashMap<String, Integer> match = new HashMap<>();
Judge() {
// 勝敗表の設定
match.put("グー グー", 0);
match.put("グー チョキ", 1);
match.put("グー パー", -1);
match.put("チョキ_グー", -1);
match.put("チョキ_チョキ", 0);
match.put("チョキ_パー", 1);
match.put("パー グー", 1);
match.put("パー チョキ", -1);
match.put("パー パー", 0);
}
public void StartJunken(Player player1, Player player2, int times) {
System.out.println("ジャンケンを開始します");
for (int i = 0; i < times; i++) {
System.out.println(i + "回戦");
final Player winner = play(player1, player2);
if (winner == null) {
System.out.println("引き分けです");
} else {
System.out.println(winner.getName() + "が勝ちました");
}
}
System.out.println("じゃんけんを終了しました!");
Player finalWinner = judgeScore(player1, player2);
if (finalWinner == null) {
System.out.println("引き分けです!");
} else {
System.out.println(finalWinner.getName() + "の勝ちです!");
}
}
Player play(Player player1, Player player2) {
Hand hand1 = player1.nextHand();
Hand hand2 = player2.nextHand();
String key = hand1.getName() + " " + hand2.getName();
System.out.println(key); // 互いの手を表示する
int result = Judge.match.get(key);
final Player ret;
if (result == 0) {
ret = null; // 引き分け
} else if (result == 1) {
ret = player1; // player1 の勝ち
} else {
ret = player2; // player2 の勝ち
}
player1.notifyResult(result);
player2.notifyResult(result * (-1));
return ret;
}
Player judgeScore(Player player1, Player player2) {
final Player wineer;
int player1Wincount = player1.score("win");
int player2Wincount = player2.score("win");
if (player1Wincount > player2Wincount) {
wineer = player1;
} else if (player1Wincount < player2Wincount) {
wineer = player2;
} else {
wineer = null;
}
return wineer;
}
}
public class Janken {
public static void main(String[] args) {
// 審判
Judge saito = new Judge();
// プレーヤー
Player yamada = new Player("山田さん");
Player murata = new Player("村田さん", new RandomTactics());
// 対戦の開始 3 回勝負
saito.StartJunken(yamada, murata, 3);
System.out.println(yamada.scoreStr());
System.out.println(murata.scoreStr());
}
}
実行例:
ジャンケンを開始します
0回戦
グー チョキ
山田さんが勝ちました
1回戦
グー グー
引き分けです
2回戦
グー パー
村田さんが勝ちました
じゃんけんを終了しました!
引き分けです!
山田さん 勝ち:1 負け:1 引分:1
村田さん 勝ち:1 負け:1 引分:1
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
0
janken.Player.RandomTacitcs を作らなければいけないのに、
janken.RandomTacitcs を作ろうとしてるので、そんなclassありませんよ、とエラーを出しているのでしょう。
ちなみに コードを記載する際には、用語統一することを心がけてください。
英単語の "tactics" を コード上では tactis と書いてあったり tacitcs と書いてあったりしていますね。
誤字だから悪い、と言っているのではなく、同じ概念を別の表記で扱うことは、
この瞬間のあなたが理解できても、他人やすぐ将来のあなたには分からなくなって
しまいかねない、という点で、注意したいものですね。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.22%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2015/05/11 21:22
説明もつけてくださり、とても見やすいです。
知識が乏しく、enum、HashMapのあたりを調べながら理解いたします。