Advertisement
cd62131

NumberGuess

Feb 24th, 2014
1,376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.29 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class NumberGuess {
  4.   public static void main(String args[]) {
  5.     Scanner in = new Scanner(System.in);
  6.     Random r = new Random();
  7.     int trial = 0, totalTrial = 0, trialMin = Integer.MAX_VALUE, game = 0;
  8.     System.out.println("1~100の数当てゲーム");
  9.     while (true) {
  10.       game++;
  11.       int computer = r.nextInt(100);
  12.       trial = 0;
  13.       while (true) {
  14.         trial++;
  15.         System.out.print("1~100の数字を入力してください: ");
  16.         int player = in.nextInt();
  17.         if (computer > player) {
  18.           System.out.println("CPUが作成した数字はもっと大きいです");
  19.           continue;
  20.         }
  21.         else if (computer < player) {
  22.           System.out.println("CPUが作成した数字はもっと小さいです");
  23.           continue;
  24.         }
  25.         System.out.println(trial + "回目で当てました");
  26.         break;
  27.       }
  28.       totalTrial += trial;
  29.       if (trialMin > trial) trialMin = trial;
  30.       System.out.print("続けますか (y / n): ");
  31.       String y_or_n = in.next();
  32.       if (y_or_n.equals("n")) break;
  33.     }
  34.     System.out.println("Game: " + game + ", Trial: " + totalTrial + ", Average: " +
  35.       totalTrial / game + ", TrialMinimum: " + trialMin);
  36.     in.close();
  37.   }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement