ジャンケン
G | C | P | |
G | 2 | 1 | 0 |
C | 0 | 2 | 1 |
P | 1 | 0 | 2 |
//ジャンケンの勝ち負けの判断 //表の活用 //入力用パッケージ import java.io.*; public class jyanken { public static void main(String[] args) { int ha,hb,rsl; String ca,cb; int tbl[][] = {{2,1,0},{0,2,1},{1,0,2}};//勝ち負け表 String st[] = {"勝ち","負け","引き分け"};//結果の文字 //一行入力準備 InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); try{//エラーチェック //Aの手を読む System.out.print("A 0 1 2:"); ca=br.readLine(); ha = Integer.parseInt(ca);//整数に変換 System.out.print("B 0 1 2:"); cb=br.readLine(); hb = Integer.parseInt(cb); rsl= tbl[hb][ha];//表から判断 System.out.println("Aの " + st[rsl]);//結果の表示 } catch(IOException e) { System.out.println("入力エラー"); e.printStackTrace(); } catch(NumberFormatException e) { System.out.println("0,1,2 の数字を入力し、Enterキーを押してください"); e.printStackTrace(); } } }実行結果
A 0 1 2:2 B 0 1 2:0 Aの 勝ち
A 0 1 2:0 B 0 1 2:e 0,1,2 の数字を入力し、Enterキーを押してください java.lang.NumberFormatException: For input string: "e" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at jyanken.main(jyanken.java:30)