計算機隨機生成一個1-100以內的數字,在7次以內幣能夠猜中java
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+"次"); } }