Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class NumberGuess {
- public static void main(String args[]) {
- Scanner in = new Scanner(System.in);
- Random r = new Random();
- int trial = 0, totalTrial = 0, trialMin = Integer.MAX_VALUE, game = 0;
- System.out.println("1~100の数当てゲーム");
- while (true) {
- game++;
- int computer = r.nextInt(100);
- trial = 0;
- while (true) {
- trial++;
- System.out.print("1~100の数字を入力してください: ");
- int player = in.nextInt();
- if (computer > player) {
- System.out.println("CPUが作成した数字はもっと大きいです");
- continue;
- }
- else if (computer < player) {
- System.out.println("CPUが作成した数字はもっと小さいです");
- continue;
- }
- System.out.println(trial + "回目で当てました");
- break;
- }
- totalTrial += trial;
- if (trialMin > trial) trialMin = trial;
- System.out.print("続けますか (y / n): ");
- String y_or_n = in.next();
- if (y_or_n.equals("n")) break;
- }
- System.out.println("Game: " + game + ", Trial: " + totalTrial + ", Average: " +
- totalTrial / game + ", TrialMinimum: " + trialMin);
- in.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement