猜数字小游戏

来源:互联网 发布:java参数构造 编辑:程序博客网 时间:2024/06/05 02:08

/*
游戏说明:
1.在1到100之间,电脑先随机出一个数(一次)
2.利用扫描器类用户开始猜(多次)
3.直到猜对为止,记录用户猜对用了多少次
4.根据所用的次数,做出不同的打印
*/
public class 猜数字 {
public static void main(String[] args) {
//1.
Random ran = new Random();
int A = ran.nextInt(100)+1;
Scanner s = new Scanner(System.in);
int sum = 0;//这个变量用于计数

while(true){
sum++;
//2.
int B = s.nextInt();//扫描之后返回用户输入的整数
//3.
if(B>A){
System.out.println("猜大了!");
}else if(B<A){
System.out.println("猜小了!");
}else if(B==A){
System.out.println("恭喜您,中了!");
break;
}
}
//4.
System.out.println("一共猜了"+sum+"次");
}


}
0 0
原创粉丝点击