本帖最後由 孫家祥 於 2012-10-10 20:24 編輯
- package sun.java.test;
- import java.io.Console;
- import java.util.Scanner;
- public class gsnum {
- public static void main(String args[])
- {
- Scanner scanIn=new Scanner(System.in);
- int v,w,x=5000,y,z,int1,int2;
- String str="請開始猜整數~",str2;
- y=1;
- v=((int)(Math.random() * 3000)+30+y);
- w=((int)(Math.random() * 6)+y);
- v=(v-(v%w));
- int1=(v-(int)(Math.random() * 30));
- int2=(v+(int)(Math.random() * 30));
- System.out.println("請輸入介於"+int1+"至"+int2+"中,"+w+"倍數之整數");
- z=0;
- while (x!=v && z<9)
- {
- System.out.println(str);
- x=Integer.parseInt(scanIn.next());
- str=(x>v)? "過大了,請再猜~介於"+int1+"至"+(x-1)+"中,"+w+"倍數之整數":"過小了,請再猜~介於"+(x+1)+"至"+int2+"中,"+w+"倍數之整數";
- z+=1;
- }
- str2=(((z==9)? "很抱歉!已猜錯"+z+"次!!":"恭喜您猜對了,沒錯!")+"正確數值是: "+v);
- System.out.println(str2);
- System.out.println("你總共猜了"+z+"遍");
- }
- }
複製代碼 |