- package mhjhmtc;
- import java.util.Scanner;
- public class Mhjhmtc {
- public static void main(String[] args) {
- // TODO 自動產生的方法 Stub
- int answer = (int) (Math.random() * 99 + 1);
- int low = 1;
- int high = 100;
- System.out.println("請輸入" + low + "~" + high + "的數");
- int times = 0;
- int guess=0;
- while (true) {
- try {
- Scanner scanner = new Scanner(System.in);
- guess = scanner.nextInt();
- }
- catch (Exception ex) {
- guess=0;
- }
- if (guess < answer) {
- low = guess;
- System.out.println("數字太小了喔");
- System.out.println("請輸入" + low + "~" + high + "的數");
- times++;
- }
- else if (guess > answer) {
- high = guess;
- System.out.println("數字太大了喔");
- System.out.println("請輸入" + low + "~" + high + "的數");
- times++;
- }
-
- else {
- System.out.println("答對了");
- System.out.println("總共達對了" + times);
- }
- }
- }
- }
複製代碼 |