A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© marsfan 中级黑马   /  2016-9-29 23:48  /  1077 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.*;
class  猜拳 {
        public static void main(String[] args) {
                /*
                        猜拳游戏
                */
                 // 电脑 随机 出拳;
                 Scanner in = new Scanner (System.in);
                 int r = 0;
                 int d = 0;
                 while(true){
                 int a = getSJS1();
                 
                 // --------------------------
                 System.out.println("请你出拳");
                 String t =in.next();
                 //    t       a   
                 if((t.equals("剪刀") && a ==3)|| (t.equals("石头") && a ==2) || (t.equals("布")&& a ==1)){
                                System.out.print("你赢了");
                                r++;
                 }else if ((t.equals("剪刀")&&a==2)|| (t.equals("石头")&& a ==1) || (t.equals("布")&& a ==3)){
                                System.out.print("平局");
                                r++;
                                d++;
                 }else{
                                System.out.print("电脑赢了");
                                d++;
                 }
                 if(r == 2 || d == 2  ){
                        break;
                 }
               
                 if(r > d){
                        System.out.print(" 最终的 赢家是 人" );

                 }else if(r < d){
                        System.out.print(" 最终的 赢家是电脑" );
                 }else{
                        System.out.print(" 最终的 结果是平局" );
                 }

        }
        public static int getSJS(){
                Random r =new Random();
                return r.nextInt(100)+1;
        }
        public static int getSJS1(){
                Random r =new Random();
                return r.nextInt(3)+1;
        }
}

4 个回复

倒序浏览
这题明显有套路,跟我们助教当时讲的一样,等学了集合可以优化好多
回复 使用道具 举报
回复 使用道具 举报
8888888888888888
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马