• 猜数字大小游戏,用户输入一个数字,如果大了就显示大了,如果小了就显示小了, 如果对了就提示正确(补充难度,只有5次机会,限制数字的范围在百位以内)


    产生0-100之间的随机数,包括0和100

    double d = Math.random() * 100;

    int a = (int)Math.round(d);

    -------------------------------------

    package 水仙花数;

    import java.util.Scanner;
    /* 猜数字大小游戏,用户输入一个数字,如果大了就显示大了,如果小了就显示小了,
    如果对了就提示正确(补充难度,只有5次机会,限制数字的范围在百位以内)
    */
    public class test2 {
    public static void main(String[] args) {
      int n = (int) Math.round(Math.random() * 100);
      System.out.print("请输入一个0-100的整数:");
      Scanner in = new Scanner(System.in);
      for(int i = 4; i >=0; i--) {
        int a = in.nextInt();
        if(a > 100 || a < 0) {
          System.out.println("输入有误,请重输");
          continue;
        }
        if(a > (n + 5)) {
          System.out.print("字过大 还有" + i +"次机会");
          continue;
        }
        if( a < (n - 5)) {
          System.out.print("字过小 还有" + i +"次机会");
          continue;
        }
        if (a == n) {
          System.out.println("恭喜你!猜对啦!");
          break;
        }
      }
       in.close();
    }
    }

    Tips: 1、产生0-100之间的随机数 int a = Math.round(Math.random() * 100)); 

        2、使用for(i=4; i >=0; i++)来逆向循环,提示还有多少次输入机会;

        3、通过continue来跳过之后的代码,进入下一次循环

  • 相关阅读:
    Marriage Match II 【HDU
    Leapin' Lizards [HDU
    稳定婚姻匹配问题
    Sabotage 【UVA
    动态树 学习
    Minimum Cost 【POJ
    Colourful Rectangle【扫描线】
    Get The Treasury【HDU-3642】【扫描线】
    Picture【HDU
    洛谷P1457 城堡 The Castle
  • 原文地址:https://www.cnblogs.com/zjulanjian/p/10189833.html
Copyright © 2020-2023  润新知