java隨機數的產生
int number=(int)(Math.random()*10+1)
Math.random()*n //n個隨機數,從0開始
do{}while循環
//猜數,1到10的隨機數,猜一個數,判斷是否猜中,若為猜中繼續直至猜中為止
public static void main(String[] args) {
int number=(int)(Math.random()*10+1);//產生隨機數
int guess;
System.out.println("請輸入一個1到10之間的整數");
do{
System.out.println("請輸入要猜測的數");
Scanner input = new Scanner(System.in);
guess=input.nextInt();
if(guess>number){
System.out.println("太大了");
}else if(guess<number){
System.out.println("太小了");
}
}while(guess!=number);
System.out.println("恭喜你猜對了");
}
while 循環
public static void main(String[] args) {
// Scanner input = new Scanner(System.in);
// int t=1+(int)(Math.random()*10);//產生隨機數
// System.out.println("要求猜一個介於1至10之間的數");
// System.out.println("請輸入您猜測的數");
// int i = input.nextInt();
// if (i == t) {
// System.out.println("您猜中了,答案為:" + t);
// }
// while(i!=t){
// if (i < t) {
// System.out.println("太小了");
// }
// else if (i > t) {
// System.out.println("太大了");
// }
// int j=input.nextInt();
// i=j;
// if(j==t){
// System.out.println("您猜中了,答案為:" + t);
// }
//
//
// }
//
// }
