import java.util.*;
public class Lab_8 {
public static void main(String[] args) {
Scanner keyin = new Scanner(System.in);
byte number = 0;//初始值
byte RANDOM = (byte)(Math.random()*49+1);
//Math.random()會產生0~1之間的亂數
System.out.println(RANDOM);
byte count = 5;
System.out.println("請輸入1~50之間的整數:");
System.out.println("您只有5次機會:");
number = keyin.nextByte();
do{
if(number > RANDOM){
System.out.println("再小一點!");
--count;
System.out.println("你還有"+count+"次機會");
number = keyin.nextByte();
}else if(number < RANDOM){
System.out.println("再大一點!");
--count;
System.out.println("你還有"+count+"次機會");
number = keyin.nextByte();
}
}while( number != RANDOM && count > 1);
if (number != RANDOM){
System.out.println("你太遜啦!");
}else{
--count;
System.out.println("你還有"+count+"次機會");
System.out.println("你真厲害!");
}
}
}
##### source code ends ###############################
2009年1月14日 星期三
Java小練習(9)
本程式要作一個猜數字的遊戲
作法及提示:
1. 請使用 Math.random() 產生介於 1 ~ 50 之間亂數值
2. 接受玩家輸入值之後進行判斷
3. 提示比較後的值是過大還是過小
執行後類似:
輸入介於 1 ~ 50 之間亂數: 26
數字太小、請增加
輸入介於 1 ~ 50 之間亂數: 40
數字太大、請減少
輸入介於 1 ~ 50 之間亂數: 35
數字太小、請增加
輸入介於 1 ~ 50 之間亂數: 37
恭禧您猜對了!
##### source code starts #############################
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言