在通过玩家选择一个商品,猜价格,猜错了,来声明猜多了还是猜少了, java小作业,猜价格游戏,输入错误给出提示 import java.util.Scanner; public class Caicaicai { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String username; float price=0.00f; System.out.println("--------------------"); System.out.println(" 猜价格 "); System.out.println(" 第一版"); System.out.println(" 制作人 zw"); System.out.println("===================="); scanner.nextLine(); System.out.println("请输入用户名"); username= scanner.next(); System.out.println("--------------------"); System.out.println(" 选择商品类别 "); System.out.println("1,古董花瓶 1-500000 "); System.out.println("2.宝石项链 1-500000 "); String name_shangpin=scanner.next(); switch (name_shangpin){ case "1": price=Math.round(Math.random()*500000); break; case "2": price=Math.round(Math.random()*500000); break; default: price=999999999; } System.out.println("请玩家输入商品价格"); float user_price=scanner.nextFloat(); boolean flag=false; while (!(user_price==price)){ if (user_price<=0) { flag = true; break; } if(user_price>price){ System.out.println("价格猜多了,猜少一点吧"); }else if (user_price<price){ System.out.println("价格猜少了,猜多一点吧"); } user_price= scanner.nextFloat(); } if (flag){ System.out.println("再接再厉"); }else { System.out.println("你真厉害"); } System.out.println("有更多想看的轻轻前往www.sdasdadsasd.com"); } } 代码思想:通过循环和if语句来判断用户想不想退出游戏,如果不想退出判断是否猜对,若没有猜对声明给用户猜多还是猜少了,让用户接着猜。
练习循环 猜价格小游戏
于 2023-10-31 14:52:14 首次发布