import java.util.Scanner; public class GuessNumberGame { Scanner sc = new Scanner(System.in); public static void main(String[] args) { Scanner sc = new Scanner(System.in); //請輸入你所猜的數字: System.out.println("請輸入你所猜的0 -- 100之間的整數: "); double n = sc.nextDouble(); guessNumber(n); } //猜數游戲數據 public static void guessNumber(double n){ int num = (int)(Math.random()*101); Scanner sc = new Scanner(System.in); for(int i = 10 ; i > 0 ;){ if(n >= 0 && n <= 100 && n % 1 == 0){ if(n == num){ System.out.println("恭喜猜對, 獎品是一個娃娃!"); break; }else{ i--; if(i == 0){ System.out.println("你猜錯了,你的機會已用完"); break; }else{ System.out.println("你猜錯了"); //判斷猜的數大了還是小了 if(n > num){ System.out.println("猜大了,你還有"+i+"次機會"); }else{ System.out.println("猜小了,你還有"+i+"次機會"); } } } }else{ System.out.println("數據錯誤!"); } System.out.println("請重新輸入數字:"); n = sc.nextDouble(); } } }