1 import java.util.Random; //調用隨機數包 2 import java.util.Scanner; 3 public class CSZ_demo{ 4 public static void main(String args[]){ 5 Random ran = new ... ...
1 import java.util.Random; //調用隨機數包 2 import java.util.Scanner; 3 public class CSZ_demo{ 4 public static void main(String args[]){ 5 Random ran = new Random(); 6 int i = ran.nextInt(10); //隨機數賦值給i 7 Scanner sc = new Scanner(); 8 while(ture){ 9 int j = sc.nextInt(System.in); //j接收來自鍵盤的輸入 10 if(j<i){ 11 System.out.println("猜小了"); 12 } else if(j>i){ 13 System.out.println("猜大了"); 14 }else{ 15 System.out.println("猜對了"); 16 break; //猜對break跳出迴圈 17 } 18 19 } 20 21 } 22 }