zoukankan      html  css  js  c++  java
  • JAVA Fundamental--20150804

     1 package day04;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Fundamental {
     6     
     7 
     8 
     9     
    10     public static void main(String[] args) {
    11         
    12         System.out.println("欢迎尝试猜字母游戏");
    13          
    14         Scanner scan = new Scanner(System.in);
    15         //String arr = scan.next();
    16         char[] input =null;
    17         //input = arr.toCharArray();
    18         char[] str=Guess();
    19         for(int k=0;k<5;k++)
    20         System.out.print(str[k]);
    21         int count=0;
    22         System.out.println("游戏开始,请输入你猜的五个字母序列:(exit! 退出)");
    23         while(true){
    24             String arr = scan.next().trim().toUpperCase();
    25             if(arr.equals("EXIT!")){
    26                 System.out.println("游戏结束!");
    27                 break;
    28             }
    29             input = arr.toCharArray();
    30             int[] sum= check(str,input);
    31             int score=500-10*count; 
    32             if(sum[0]==5){
    33                 System.out.println("恭喜你猜对了,你的得分是:"+ score);
    34                 
    35             }
    36             else{
    37                 System.out.println("你猜对"+sum[1]+"个字符"+",其中有"+sum[0]+"个字符的位置正确");
    38                 
    39             }
    40             count++;
    41             //check();
    42             //Guess();
    43         }
    44      
    45     }
    46     
    47     
    48     public static char[] Guess(){
    49         
    50         char[] str = new char[5];
    51          char[] zmb={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
    52                 'Q','R','S','T','U','V','W','X','Y','Z'};
    53          boolean[] flag = new boolean[zmb.length];
    54          
    55         for(int i=0;i<str.length;i++){
    56            int  num;
    57         do{
    58               num = (int)(Math.random()*zmb.length);
    59              
    60              
    61         }while(flag[num]);
    62         str[i]=zmb[num];
    63         flag[num]=true;
    64         }
    65         return str;
    66     }
    67     
    68     public static int[] check(char[] str,char[] input){
    69         int[] sum = new int[2];
    70         for(int i=0;i<5;i++){
    71             for(int j=0;j<5;j++){
    72                 if(str[i]==input[j]){
    73                     sum[1]+=1;
    74                     if(i==j){
    75                         sum[0]+=1;
    76                         
    77                     }
    78                     break;
    79                 }
    80                 
    81                 
    82             }
    83             
    84         }
    85         return sum;
    86     }
    87 
    88 
    89 }
  • 相关阅读:
    tuple 元组及字典dict
    day 49 css属性补充浮动 属性定位 抽屉作业
    day48 选择器(基本、层级 、属性) css属性
    day47 列表 表单 css初识
    day 46 http和html
    day 45索引
    day 44 练习题讲解 多表查询
    day 40 多表查询 子查询
    day39 表之间的关联关系、 补充 表操作总结 where 、group by、
    day38 数据类型 约束条件
  • 原文地址:https://www.cnblogs.com/xiaziteng/p/4703422.html
Copyright © 2011-2022 走看看