/*
* 1. 输入0,直接退出游戏
* 2. 记录成绩 1-3 A 4-6 B 7-10 C
* 3. 超过10次就直接退出游戏,永久封号
* 4. 输入游戏难度 1 1-50 2 1-100 3 1-200
*/
package com.qf;
import java.util.Scanner;
public class Demo03{
public static void main(String[] args) {
//定义扫描器
Scanner scan = new Scanner(System.in);
//提示用户输入游戏等级
System.out.println("请输入游戏等级 1容易 2普通 3困难 :");
int lever = scan.nextInt();
//自动生成[1,100]的数字
int num;
String scope;
if(lever == 1) {
num = (int) (Math.random()*50+1); //[1,50]
scope = "[1-50]";
} else if(lever == 2) {
num = (int) (Math.random()*100+1); //[1,100]
scope = "[1-100]";
} else {
num = (int) (Math.random()*200+1); //[1,200]
scope = "[1-200]";
}
//提示用户输入
System.out.println("请输入您猜的数字"+scope+":");
//扫描器扫描得到数字
int guessNum = scan.nextInt();
int count = 1; //定义变量,记录猜的次数
//没有猜对的时候,继续猜
while(num != guessNum) {
if(guessNum == 0) { // 主动退出
break;
}
if (guessNum > num) {
System.out.println("大了,请输入您猜的数字"+scope+":");
} else {
System.out.println("小了,请输入您猜的数字"+scope+":");
}
//使用guessNum接收用户再次输入的值,进行判断
guessNum = scan.nextInt();
if(++count >10) { //超过10次,主动退出
break;
}
}
if(guessNum == 0) {
System.out.println("退出");
} else {
if(count<=3) {
System.out.println("A你猜对了");
} else if(count <=6) {
System.out.println("B你猜对了");
} else if(count <=10) {
System.out.println("C你猜对了");
} else {
System.out.println("永久封号!!");
}
}
}
}
最后
以上就是苹果小蚂蚁最近收集整理的关于java基础——使用循环设计猜数字小游戏的全部内容,更多相关java基础——使用循环设计猜数字小游戏内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复