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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© x837248435 中级黑马   /  2016-7-21 17:44  /  485 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.Scanner;
class Day07HomeWork3 {
        public static void main(String[] args) {
                Scanner sc=new Scanner(System.in);
               
               
                //1
                for (; ; ) {
                        int a=((int)(Math.random()*100)-1)/33;
                        System.out.println("请输入一个整数代表你的手掌,0=石头,1=剪刀,2=布");
                        int b=sc.nextInt();
                        if(a==b) {
                                System.out.println("平手");
                        }else if (a==0 &&b==1) {
                                System.out.println("输了");       
                        }else if (a==0 &&b==2) {
                                System.out.println("赢了");
                        }else if (a==1 &&b==0) {
                                System.out.println("赢了");
                        }else if (a==1 &&b==2) {
                                System.out.println("输了");
                        }else if (a==2 &&b==0) {
                                System.out.println("输了");
                        }else if(b>2 || b<0) {
                                System.out.println("能不能好好玩");
                        }else {
                                System.out.println("赢了");       
                        }
                }
        }
}

1 个回复

倒序浏览
import java.util.Scanner;
class Test {
        public static void main(String[] args) {
                Scanner sc=new Scanner(System.in);
                                int count = 0;
                for (; ;) {
                                                int a=((int)(Math.random()*3)+1);
                        System.out.println("请输入一个整数代表你的手掌,1=石头,2=剪刀,3=布");
                        int b=sc.nextInt();
                                                if(b<1||b>3){
                                                        System.out.println("输入错误");
                                                        System.exit(0);
                                                }
                        if(a==b) {
                                System.out.println("平手");
                        }else if ((Math.abs(a-b)==2&&b>a)||(Math.abs(a-b)==1&&a>b)) {
                                                                System.out.println("赢了");
                        }else{
                                                                System.out.println("输了");
                                                                count++;
                                                }
                                                if(count==10){
                                                System.out.println("笨蛋,输10次就不能玩了");
                                                }
                                }       
            }
       }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马