解答例 - 実習課題2 - 7.数値処理/数値の表現
(実習課題2)
コンピュータとのジャンケンを実現する、コンソールプログラムを作成しなさい。
解答例
package com.techscore.utility.chapter7.exercise2;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
/**
* PaperStoneScissorsExample.java
* TECHSCORE Javaユーティリティ7章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
public class PaperStoneScissorsExample {
public static void main(String[] args) {
printUsage();
Random random = new Random();
Janken.Hand computer = null;
Janken.Hand player = null;
int win = 0;
int lost = 0;
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
try {
String line = null;
while ((line = reader.readLine()) != null && !".".equals(line)) {
//コンピュータの手を乱数で決定する。
computer = getHand(random.nextInt(3));
if (!line.matches("[0-2]")) {
printError();
continue;
} else {
player = getHand(Integer.parseInt(line));
}
System.out.println("---------------------------");
System.out.println("じゃんけんぽん");
System.out.println(" あなたの手 :" + player.name + " ");
System.out.println(" コンピュータの手:" + computer.name);
switch (Janken.poi(computer, player)) {
case Janken.DRAW :
System.out.println("結果:あいこ");
break;
case Janken.LOST :
System.out.println("結果:コンピュータの勝ち");
lost++;
break;
case Janken.WIN :
System.out.println("結果:あなたの勝ち");
win++;
break;
}
System.out.println("---------------------------");
System.out.println("あなたの " + win + " 勝 " + lost + " 敗です。");
}
System.out.println("またね。");
} catch (Exception e) {
e.printStackTrace();
}
}
private static Janken.Hand getHand(int num) {
switch (num) {
case 0 :
return Janken.STONE;
case 1 :
return Janken.PAPER;
case 2 :
return Janken.SCISSORS;
default :
printError();
return null;
}
}
private static void printUsage() {
System.out.println("0:グー、1:パー、2:チョキ、.:終了");
}
private static void printError() {
System.out.println("入力値が不正です。");
printUsage();
}
}
/**
* じゃんけんを定義するクラス
*/
class Janken {
static Hand STONE = new Hand("グー", 0);
static Hand PAPER = new Hand("パー", 1);
static Hand SCISSORS = new Hand("チョキ", 2);
static final int WIN = 1;
static final int LOST = -1;
static final int DRAW = 0;
static int poi(Hand player, Hand com) {
switch (player.number - com.number) {
case 0 :
return Janken.DRAW;
case -1 :
return Janken.LOST;
case 2 :
return Janken.LOST;
default :
return Janken.WIN;
}
}
static class Hand {
String name = null;
int number = -1;
private Hand(String name, int number) {
this.name = name;
this.number = number;
}
}
}

