猜数游戏
数的范围是[1,101]
代码
import java.util.Scanner;
public class Guessnumbergame {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int number = (int) (Math.random()*100+1);//[0,1]-->[1,100]-->[1,101]
int a;
int count = 0;
do{
a=sc.nextInt();
if (a>number) {
System.out.println("偏大了哦");
count++;
}
else if (a<number) {
System.out.println("偏小了哦");
count++;
}
}while(a!=number);
System.out.println("恭喜你猜对了,你猜了"+count+"次");
}
}
运行结果