新手求大神解,这两者的不同,和优劣。虚心求教!!

Yanpevg 2015-08-30 03:40:01

import java.util.*;
public class game{
public static void main(String[] arges){
System.out.println("-----猜拳游戏-----");
System.out.println("1石头,2剪刀,3布");
System.out.println("请输入");
Scanner in=new Scanner(System.in);
Random number=new Random();
int number3=number.nextInt(3)+1;
int number1=in.nextInt();
if(number1==1&&number3==3){
System.out.println("你出的石头,电脑是布,-_-你输了");
}else if(number1==1&&number3==2){
System.out.println("你出的石头,电脑是剪刀,^_^你赢了");
}else if(number1==1&&number3==1){
System.out.println("你出的石头,电脑是石头,平局");
}else if(number1==2&&number3==2){
System.out.println("你出的剪刀,电脑是剪刀,平局");
}else if(number1==2&&number3==3){
System.out.println("你出的剪刀,电脑是布,^_^你赢了");
}else if(number1==2&&number3==1){
System.out.println("你出的剪刀,电脑是石头,-_-你输了");
}else if(number1==3&&number3==3){
System.out.println("你出的布,电脑是布,平局");
}else if(number1==3&&number3==2){
System.out.println("你出的布,电脑是剪刀,-_-你输了");
}else if(number1==3&&number3==1){
System.out.println("你出的是布,电脑是石头,^_^你赢了");
}

}
}
...全文
106 1 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zhuangqingch 2015-08-31
1、从运行效率上看,前者与后者相比判断次数较少一些(其实也只少了一点点,else if中用了几个或||),所以会快一点点。 2、从代码简洁及可读性上看,前者相比后者会简洁一些,可读性也要比较后者要好且易理解,后者判断太多,虽然穷举出所有可能的判断,但有时难免会有一些遗漏。 根据楼主想要实现的效果,对楼主的代码做了点调整,楼主可以参考下: 第一种实现(优化输出描述):

import java.util.Scanner;
/**
 * @author zhuangqing
 */
public class Test {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] msg = {"剪刀","石头", "布"};
        System.out.println("-----猜拳游戏-----");
        System.out.println("请出拳(1剪刀,2石头,3布)");
        int person = in.nextInt();
        int computer = (int)(Math.random()*3) + 1;
        String result = "电脑出的是:" + msg[computer-1] + ",你出的是:" + msg[person - 1];
        if (person == computer) {
            System.out.println("平局," + result);
        } else if (person == 1&&computer==2||person==2&&computer==3||person==3&&computer==1) {
            System.out.println("你输了," + result);
        } else {
            System.out.println("你赢了," + result);
        }
    }
}
第二种实现(优化判断次数):

import java.util.Scanner;

/**
 * @author zhuangqing
 */
public class Test2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] msg = {"剪刀","石头", "布"};
        System.out.println("-----猜拳游戏-----");
        System.out.println("请出拳(1剪刀,2石头,3布)");
        int person = in.nextInt();
        int computer = (int)(Math.random()*3) + 1;
        String result = "电脑出的是:" + msg[computer-1] + ",你出的是:" + msg[person - 1];
        int naturalEnemy = (person+1)%3; //天敌
        if (naturalEnemy == computer) {
            System.out.println("你输了," + result);
        } else if (person == computer) {
            System.out.println("平局," + result);
        } else {
            System.out.println("你赢了," + result);
        }
    }
}

第三种实现:(优化面向对象设计)

import java.util.Scanner;
/**
 * @author zhuangqing
 */
public class Test3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        GuessingResult[] results = {GuessingResult.SCISSOR, GuessingResult.ROCK, GuessingResult.PAPER};
        System.out.println("-----猜拳游戏-----");
        System.out.println("请出拳(1剪刀,2石头,3布)");
        int person = in.nextInt();
        int computer = (int)(Math.random()*3) + 1;
        GuessingResult personResult = results[person - 1];
        GuessingResult computerResult = results[computer-1];
        String result = "电脑出的是:" + computerResult.getDesc() + ",你出的是:" + personResult.getDesc();

        if (personResult.naturalEnemy() == computerResult) {
            System.out.println("你输了," + result);
        } else if (personResult.defeatedByMe() == computerResult) {
            System.out.println("你赢了," + result);
        } else {
            System.out.println("平局," + result);
        }
    }
}

/**
 * 猜拳结果枚举类
 */
enum GuessingResult {
    SCISSOR("剪刀"){
        public GuessingResult  naturalEnemy() {
            return ROCK;
        }
        public GuessingResult defeatedByMe() {
            return PAPER;
        }
    },
    ROCK("石头"){
        public GuessingResult  naturalEnemy() {
            return PAPER;
        }
        public GuessingResult defeatedByMe() {
            return SCISSOR;
        }
    },
    PAPER("布"){
        public GuessingResult  naturalEnemy() {
            return SCISSOR;
        }
        public GuessingResult defeatedByMe() {
            return ROCK;
        }
    },
    ;
    private String desc;
    GuessingResult(String desc) {
        this.desc = desc;
    }

    public String getDesc() {
        return desc;
    }

    /**
     * 天敌
     * @return
     */
    public abstract GuessingResult  naturalEnemy();

    /**
     * 能击败的对象
     * @return
     */
    public abstract GuessingResult defeatedByMe();
}
就这些吧,希望这些代码能对楼主有所帮助,有不明白的,欢迎追问!
回复
前者代码节俭,会节省很多运行成本的!
回复
相关推荐
发帖
Java SE
创建于2007-09-28

6.2w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2015-08-30 03:40
社区公告
暂无公告