zoukankan      html  css  js  c++  java
  • 猜数游戏

    package pro2;
    import java.util.*;
    public class Random01 {
      public static void main(String[] args) {
        System.out.println(" 开始游戏:");
        game();
        System.out.println("游戏结束:");
      }
      public static void game(){
        boolean bool=true;
        while(bool==true){
          int ran=random();
          for(int i=0;i<10;i++){
            Scanner sc=new Scanner(System.in);
            System.out.print("请输入一个数字用于比较:");
            int flag=sc.nextInt();
            int key=compare(flag,ran);
            if(key==0){
            System.out.println("恭喜猜数正确,本轮游戏结束");
            break;
            }
            else if(key>0){
              System.out.println("您输入的数字--大 ");
            }
            else if(key<0){
              System.out.println("您输入的数字--小 ");
            }
            if(i==10){
              System.out.println("10次机会用尽,本轮游戏结束");
            }
          }
          //猜数正确跳出循环
          {
            System.out.println("是否要进行下一轮游戏(任意键继续,0退出)");
            Scanner sc2=new Scanner(System.in);
            int judge=sc2.nextInt();
            if(judge==0){
            bool=false;
            }
          }
        }
      }
      public static int random(){
        Random random=new Random();
        int i=random.nextInt(1000)+1;
        return i;
      }
      public static int compare(int flag,int ran){
        if(flag>ran){
          return 1;
        }
        else if(flag<ran){
          return -1;
        }
        return 0;
      }

    }

  • 相关阅读:
    RDA安装
    关闭SELinux
    Linux下开启关闭防火墙
    长事务管理
    增加复制表
    Python使用property函数定义的属性名与其他实例变量重名会怎么样?
    Python使用property函数定义属性访问方法如果不定义fget会怎么样?
    Python使用property函数和使用@property装饰器定义属性访问方法的异同点分析
    第8.18节 Python类中内置析构方法__del__
    Python中的列表解析和列表推导是一回事吗?
  • 原文地址:https://www.cnblogs.com/hxtblogs/p/7591315.html
Copyright © 2011-2022 走看看