import java.util.Scanner;
public class game2 {
public static void main(String[] args) {
// 定义全局变量username玩家参与名字 plan_price系统产生的价格
String username;
float plan_price=0.00f;
//定义菜单
System.out.println("--------------------------");
System.out.println("游戏:幸运价格猜猜猜!!");
System.out.println(" 作者:克莱");
System.out.println("--------------------------");
// 显示欢迎界面 当用户敲回车 进入用户名输入界面
// 这里的回车需要Scanner
Scanner scanner=new Scanner(System.in);
// 先接收回车
scanner.nextLine();
System.out.println("请输入用户名字:");
// 接收全局变量 username
username=scanner.next();
System.out.println("欢迎"+username+"进行游戏");
System.out.println("现在请选择商品的类别");
// 输出类别
System.out.println("-----------------------");
System.out.println("1-------家电(1000~2000)");
System.out.println("2-------文具(1~100)");
System.out.println("-----------------------");
// 接收用户输入 防止输入其他报错 不用nextint
System.out.println("请输入你的选择:");
String user_choice=scanner.next();
// 用户的选择输入后 只有1和2响应 其他随便
// 判断 条件有确定值用switch
System.out.println("请输入猜的价格" );
switch(user_choice){
case"1":
plan_price=Math.round(Math.random()*1000+1000);
break;
case"2":
plan_price=Math.round(Math.random()*99+1);
break;
default:
plan_price=Math.round(Math.random()*10000);
}
//用户猜数字 输入价格和判断是重复的步骤 while猜对 还有一个变量输入价格
// 如果用户输入0或负数 直接退出猜价格
float user_price=scanner.nextFloat();
// 输出不同信息的标志位
boolean flag=true;
while(!(user_price==plan_price)){
if(user_price<=0){
// 退出循环
flag=true;
System.out.println("退出成功,下次努力");
break;
}
if(user_price>plan_price){
System.out.println("猜大了");
}else{
System.out.println("猜小了");
}
// 继续猜
user_price= scanner.nextFloat();
}
if(false){
System.out.println("恭喜你猜对了!!");
System.out.println("感谢你使用,如有意见可提出,地址:123456@qq.com");
}
}
}
幸运价格猜猜猜 Java小游戏
最新推荐文章于 2024-07-22 22:58:19 发布