以下為修正版
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
int min = 1;
int max = 100;
boolean isBoo = true;
int ans = (int) (Math.random() * 100 );//1-100
do {
System.out.printf("請猜 %d ~ %d:", min, max);
int keyin = scanner.nextInt();
if (keyin == ans) {
System.out.println("猜中了!");
isBoo = false;
} else if (keyin < ans && keyin >=min) {
System.out.println("猜大一點");
min = keyin;
} else if (keyin > ans && keyin <=max) {
System.out.println("猜小一點");
max = keyin;
} else {
System.out.println("超出範圍");
}
} while (isBoo);
沒有留言:
張貼留言