猜隨機生成數

計算機隨機生成一個1-100之內的數字,在7次之內幣可以猜中

package hello;

import java.util.Scanner;

public class Caishu {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int number = (int) (Math.random() * 100 + 1);// [0,1)>>[0,100)>>[1,100]
		int a;
		
		int count = 0;
		do{
			a=in.nextInt();
			count=count+1;
			if (a > number)
			{
				System.out.println("偏大");

			}
			else if(a<=number)
			{
				System.out.println("偏小");
			}
		}while(a!=number);
		System.out.println("恭喜您猜對了,您共猜了"+count+"次");
			
		

	}
	
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章