zoukankan      html  css  js  c++  java
  • Java实验项目二——猜数游戏

     1 /*
     2  * Description:定义比较类,实现两个数的比较
     3  * 
     4  * */
     5 
     6 
     7 package compare;
     8 
     9 import dao.Operate;
    10 
    11 public class Compare {
    12 
    13     private static boolean flag = true;
    14     
    15     public Compare() {
    16         
    17         while( flag ) {
    18             
    19             int count = 0;                    //统计用户输入的次数
    20             System.out.print( "游戏开始:" );
    21             int random = Operate.getRandom();        //取得随机数
    22             System.out.println( "正确答案为  " + random );
    23             
    24             for( count = 1; count <= 10; count++ ) {        //十次机会
    25                 
    26                 int input = Operate.getInput();                //取得用户输入的数据
    27                 
    28                 switch( compare(random, input) ) {            //进行两个数的比较
    29                 
    30                     case 1:System.out.println( "输入值偏大!" );break;
    31                     
    32                     case -1:System.out.println( "输入值偏小!" );break;
    33                     
    34                     case 2:
    35                     case 0: {
    36                         Compare.flag = Compare.ifOver(count);            //判断count的值
    37                         count = 100;
    38                         break;
    39                     }
    40                         
    41                 }
    42             }
    43             
    44             if( count == 11 ) {                        //十次机会用完,判断是否结束游戏
    45                 Compare.flag = ifOver(count);
    46             }
    47                 
    48             
    49         }
    50         
    51         System.out.println( "游戏结束!" );
    52         
    53         
    54     }
    55     
    56 
    57     public static int compare(int x1,int x2) {        //定义比较方法,x1为随机数,x2为用户输入的数
    58         
    59         if( x1 < x2 ) {                //用户输入的数过大
    60             
    61             return 1;
    62         }else if( x1 > x2 ) {        //用户输入的数过小
    63             
    64             return -1;
    65         }else {                        //输入正确
    66             
    67             return 0;
    68         }
    69     }
    70     
    71     public static boolean ifOver(int count) {            //通过count的值判断是否结束游戏或者进入下一轮
    72         
    73             if( count <= 10 ) {
    74                 
    75                 System.out.println( "输入值正确!" );
    76             }
    77             else{
    78                 
    79                 System.out.println( "10次机会用完,本轮游戏结束!" );
    80             }
    81             
    82             System.out.println( "输入0结束游戏!,输入其它继续下一轮游戏!" );
    83             if( Operate.getInput() == 0 ) {
    84                 
    85                 return false;
    86             }else {
    87                 
    88                 return true;
    89             }
    90     }
    94 }

     1 /*
     2  * 定义操作类Operate:用于产生随机数和取得用户输入
     3  * 
     4  * */
     5 
     6 package dao;
     7 
     8 import java.util.Random;        //导入包
     9 import java.util.Scanner;
    10 
    11 
    12 public class Operate {
    13 
    14     //产生1-1000之间随机数
    15     public static int getRandom() {        
    16         
    17         Random random = new Random();
    18         int max = 1001;
    19         int min = 1;
    20         
    21         return random.nextInt(max) % (max - min + 1) + min;
    22         
    23     }
    24     
    25     
    26     public static int getInput() {            //返回用户猜测输入的数据
    27         
    28         Scanner scan = new Scanner(System.in);
    29         int input = 0;
    30         
    31         System.out.println("请输入:");
    32         input = scan.nextInt();
    33         
    34         return input;
    35     }
    38 }
     1 package main;
     2 
     3 import compare.Compare;
     4 
     5 public class Main {
     6 
     7     public static void main(String args[]) {
     8         
     9         new Compare();        //实例化对象,进行游戏
    10     }
    11     
    12 }
    初学小白,请多指教!
  • 相关阅读:
    过程作为黑箱抽象——《计算机程序的构造和解释》
    过程与它们所产生的计算——《计算机程序的构造和解释》
    重构手法(四)之在对象之间搬移特性
    重构手法(三)之简化条件表达式
    重构手法(二)之简化函数调用
    重构手法(一)之重新组织函数
    代码的坏味道
    泛型算法(二十三)之排列算法
    泛型算法(二十二)之集合操作算法
    泛型算法(二十一)之比较算法
  • 原文地址:https://www.cnblogs.com/caizhen/p/7591321.html
Copyright © 2011-2022 走看看