Quantcast
Viewing all articles
Browse latest Browse all 2439

java勉強中課題作成 コレクションとか

マルチスレッド、パッケージ、コレクションを使ってプログラムを考えてジャンケンで得た得点と掛け算問題で得た得点をたして最初に聞いた名前で得点を加算してMapか何かを使って最終的には、名前と得点を出力したい。10名だけ限定。(できれば高得点者順) ジャンケンと掛け算の問題は単独でできています。ただし掛算は10秒のタイム制限で、すぎると不正解にしたい。出来ているのは掛け算とジャンケンの部分のみで,joinでストップをかけたら、そこのcatchでコンパイルエラーになってます。後の合計後の得点処理は全然できてません。色々調べましがわかりません。宜しくお願いします。 import java.io.*; public class JankenKakezan extends Thread { public static void main(String[] args) throws InterruptedException { Kakezan th = new Kakezan( ); th.start( ); new Thread(new Kakezan( )).start( ); System.out.println("あなたの名前を入力してください。"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { String line = reader.readLine( ); System.out.println(line + "さん、ジャンケンと掛け算をやって貰います。"); } catch (IOException e) { System.out.println(e); } try { th.join( ); } catch (InterrputedException e) { System.out.println(e); } new JankenKakezan( ).start( ); int pc; int you = 0; int tokuten1 = 0; PcJanken ja = new PcJanken( ); for (int i = 1; i <= 10; i++) { pc = ja.janken( ); System.out.println(" グウ = 1:チョキ = 2:パー = 3 を入力する。"); try { you = Integer.parseInt(reader.readLine( )); while (you != 1 && you != 2 && you != 3 ) { System.out.println("1,2,3を入力して下さい。?"); you = Integer.parseInt(reader.readLine( )); } } catch (IOException e) { } String[] s = {("グウ"),("チョキ"),("パー")}; System.out.println(" PCは" + s[pc -1] + "あなたは" + s[you -1]); if (you == pc) { System.out.println(" 引き分け"); } else if((you == 1 && pc == 2)||(you == 2 && pc == 3)||(you == 3 && pc == 1)) { System.out.println(" あなたの勝ち"); tokuten1 = tokuten1 + 10; } else { System.out.println(" PCの勝ち"); } } System.out.println(" あなたの得点は" + tokuten1 +"点です。"); } } //ジャンケンの[グウ:1][チョキー:2][パー:3]のをランダムに返す class PcJanken { public int janken( ) { int x = (int) (Math.random( ) * 3) + 1; return x; } } class Kakezan extends Thread { public static final int QuestionNumber = 5; public void run( ) { int correctAnswer = 0; System.out.println("これから2桁x1桁の掛け算の問題を" + QuestionNumber + "問出します。"); System.out.println("10秒以内に答えないと不正解になります。"); for(int i = 0; i < QuestionNumber; i++) { boolean ok = kakezanMondai(i + 1); if (ok) { correctAnswer++; } } int tokuten2 =correctAnswer*20; System.out.println("問題は" + QuestionNumber +"問出ました。"); System.out.println("正解は" + correctAnswer +"問でした。"); System.out.println("ゲーム、「掛け算」の得点は" + tokuten2 + "点です。"); } public static boolean kakezanMondai(int question) { int x =(int)(Math.random( ) * 90) + 10; int y =(int)(Math.random( ) * 9) + 1; BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); System.out.println(question + "問目" + x + "x" + y + "=?"); try { String line = reader.readLine( ); int kaitou = Integer.parseInt(line); if (x * y == kaitou) { System.out.println("正解です。"); return true; } else { System.out.println("不正解です。"); return false; } } catch (IOException e) { System.out.println(e); } catch (NumberFormatException e) { System.out.println("入力が正しくありません。"); } return false; } } class TimeOut extends Thread { @Override public void run( ) { try { Thread.sleep(10000); } catch (InterruptedException e) { System.out.println(e); } } }

Viewing all articles
Browse latest Browse all 2439

Trending Articles