###発生している問題・エラーメッセージ
リソースが閉じられません
scannerの閉じ方がわかりません
###該当のソースコード
書いたコード
package janken;
import java.util.InputMismatchException;
import java.util.Scanner;
public class JankenTakahashi {// メインクラス
public static void main(String args[]) throws InputMismatchException {
int loseCount = 0;
int player = 0;// プレイヤーの手
while (3 > loseCount) {
System.out.println("ワイとじゃんけんやで...あなたの手を入力してください ⇒( グー:0 チョキ:1 パー:2 )");
System.out.println("コンピュータに3回/負けたらllゲーム終了です");
try {
player = newjava.util.Scanner(System.in).nextInt(3);// 入力の手
while (true) {
if (player == 0) {
System.out.println("あなたの手はグーです");
break;
} else if (player == 1) {
System.out.println("あなたの手はチョキです");
break;
} else if (player == 2) {
System.out.println("あなたの手はパーです。");
break;
} else {
System.out.println("0. 1 . 2 以外は入力しないでください");
}
break;
}
} catch (InputMismatchException e) {
System.out.println("おい! 0.1.2や ");
}finally{
player.close();
}
Computer computer = new Computer();
int c = computer.getcomputer();
Winlose.winlose(c, player);
}
}
}
class Winlose {// 勝ち負けを判定するクラスprivate static int winCount = 0;// プレイヤーの勝った
private static int loseCount = 0; // プレイヤーの負けた数
private static int winCount = 0;// プレイヤーの勝った]
// private static int playerA = 0;// プレイヤーの手
public static void winlose(int computerA, int playerA) {
switch (computerA) {
// コンピュータの手がグーの場合
case 0:
// 自分の手とコンピューターの手が同じ場合
if (0 == playerA) {
System.out.println("あいこ");
// 自分の手とコンピューターの手が同じではない場合
} else if (1 == playerA) {
// 自分の手が負けた場合
System.out.println("あなたのまけ");
loseCount++;
} else if (2 == playerA) {
System.out.println("あなたの勝ち");
winCount++;
}
break;
// コンピュータの手がチョキ
case 1:
if (1 == playerA) {
System.out.println("あいこ");
} else if (0 == playerA) {
System.out.println("あなたの勝ち");
winCount++;
} else if (2 == playerA) {
System.out.println("あなたの負け");
loseCount++;
}
break;
// コンピュータの手がパー
case 2:
if (2 == playerA) {
System.out.println("あいこ");
} else if (0 == playerA) {
System.out.println("あなたの負け");
loseCount++;
} else if (1 == playerA) {
System.out.println("あなたの勝ち1");
winCount++;
}
break;
}
if (loseCount < 3) {
System.out.println("勝った" + winCount + "回数");
System.out.println("負けた" + loseCount + "回数");
} else {
System.out.println("3回負けたので終わり");
System.exit(0);
}
}
}
class Computer {// コンピュータの手を決めるクラス
public int getcomputer() {
int computer; // コンピューターの手
computer = new java.util.Random().nextInt(3);
int gu = 0; // グー
int choki = 1; // チョキ
int pa = 2; // パー
if (computer == gu) {
System.out.println("コンピューターはグーを出した");
} else if (computer == choki) {
System.out.println("コンピュータはチョキを出した");
} else if (computer == pa) {
System.out.println("コンピュータはパーを出した");
}
return computer;
}
}

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/07 06:18