用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

猜拳游戏

2016-11-22 作者: java程序设计举报

[java]代码库

package com.getGame;
 
import java.util.Scanner;
 
public class RandomGame {
    public final static int jiandao=1;
    public final static int shitou=2;
    public final static int bu=3;
    public final static String JIANDAO="剪刀";
    public final static String SHITOU="石头";
    public final static String BU="布";
    boolean panDuan=true;
    public void open(){
        while(panDuan){
            int computer=(int)(Math.random()*3+1);
            Scanner scanner=new Scanner(System.in);
            System.out.println("开始游戏\n请出拳:");
            String b=scanner.next();
            int person=String(b);
            if(person==0){
                System.out.println("你出的什么,电脑无法验证,重新输入");
                System.out.println("是否继续开始游戏:是/不");
                String colse=scanner.next();
                if(colse.equalsIgnoreCase("是")){
                    panDuan=true;
                    continue;
                }else if(colse.equalsIgnoreCase("不")){
                    panDuan=false;
                    System.out.println("结束游戏");
                    continue;
                }else{
                    panDuan=false;
                    System.out.println("游戏结束");
                    continue;
                }
            }
            both(person,computer);
            System.out.println("是否继续开始游戏");
            String start=scanner.next();
            if(start.equalsIgnoreCase("是")){
                panDuan=true;
                continue;
            }else if(start.equalsIgnoreCase("不")){
                panDuan=false;
                System.out.println("结束游戏");
                continue;
            }else{
                panDuan=false;
                System.out.println("游戏结束");
                continue;
            }
        }
    }
    private void both(int person, int computer) {
        if(person==computer){
            System.out.println("我出的是:"+daKai(person)+",电脑出的是"+daKai(computer)+",是平局\t");
        }else if(person==jiandao&&computer==shitou||person==shitou&&computer==bu||person==bu&&computer==jiandao){
            System.out.println("我出的是:"+daKai(person)+",电脑出的是"+daKai(computer)+",我输了\t");
        }else{
            System.out.println("我出的是:"+daKai(person)+",电脑出的是"+daKai(computer)+",我赢了\t");
        }
         
    }
    private String daKai(int person) {
            String c="";
            switch(person){
            case jiandao:
                c=JIANDAO;
                break;
            case shitou:
                c=SHITOU;
                break;
            case bu:
                c=BU;
                break;
            }
        return c;
    }
    private int String(String b) {
    if(b.equals(JIANDAO)){
        return jiandao;
    }else if(b.equals(SHITOU)){
        return shitou;
    }else if(b.equals(BU)){
        return bu;
    }
     
        return 0;
    }
}
 
 
 
 
 
 
 
 
 
package com.getGame;
 
public class Main {
    public static void main(String args[]){
        RandomGame randomGame=new RandomGame();
        randomGame.open();
    }
 
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...