计算机随机生成一个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+"次"); } }
时间: 2024-10-07 00:15:17