使用Java(for循环,if判断语句)编写竞猜价格游戏

【练习】猜商品价格游戏

  • 1次猜中:奖励华为Mate X 手机一部;
  • 2-3次猜中:奖励 switch 游戏机一部;
  • 5次以内猜中:小米运动手环一个;

代码如下:

package java学习代码文件;

import java.util.Scanner;

public class caispjiage {
    public static void main(String[] args) {
        //final int price = (((int)(Math.random()*1000))%9+1)*1000;//竞猜答案为1000~9000中随机一个整千的数值
        //System.out.println(price);

        final int price = 5000;
        int guessPrice = -1;
        int count = 0;
        String prizeName = null;
        
        for(int i=0;i<5;i++){
            System.out.println("请输出竞猜价格(第"+ ++count +"次):" );
            guessPrice = new Scanner(System.in).nextInt();
            if(guessPrice == price){
                if(count == 1){
                    System.out.println("恭喜您,一次猜中!获得一等奖:华为Mate X手机一部");
                    
                }else if(count >=2&&count == 3){
                    System.out.println("恭喜您,获得二等奖:swith游戏机一部");
                    
                }else {
                    System.out.println("恭喜您,获得三等奖:小米运动手环一个");
                    
                }
                break;
            }
             //        else{
             //        System.out.println("猜错了,很遗憾!");
             //        }

            else if(guessPrice > price){
                System.out.println("猜大了,请继续");
            }else if(guessPrice < price){
                System.err.println("猜小了,请继续");
            }
        }
    }
}
 

使用Java(for循环,if判断语句)编写竞猜价格游戏

使用Java(for循环,if判断语句)编写竞猜价格游戏