当前位置: 代码迷 >> J2SE >> while循环错误
  详细解决方案

while循环错误

热度:115   发布时间:2016-04-24 01:21:02.0
while循环异常
各位好!我java刚入门,昨天写了一个走棋的小程序,遇到一些问题,在此麻烦各位帮我看下:
Java code
/** * 下棋游戏 * @author Alex * */public class Play {    private static String[] map;    //地图    private static String p1;        //玩家1    private static String p2;        //玩家2    private static String mapStyle; //地图样式    /**     * 初始化地图     * @param mapStyle    地图样式     * @param mapSize    地图大小(长度)     */    private static void initMap(String mapStyle,int mapSize){        Play.mapStyle=mapStyle;        map=new String[mapSize+1];        for(int i=0;i<map.length;i++){            map[i]=Play.mapStyle;        }    }    /**     * 显示地图     */    private static void showMap(){        for(int i=0;i<map.length;i++){            System.out.print(map[i]);        }    }    /**     * 初始化玩家位置     * @param p1Style    玩家1的样式     * @param p2Style    玩家2的样式     */    private static void initPlayer(String p1Style,String p2Style){        Play.p1=p1Style;        Play.p2=p2Style;        map[0]=Play.p1;        map[1]=Play.p2;    }    /**     * 查找玩家位置     * @param playerName    玩家名称     * @return                玩家位置     */    private static int findPlayer(String playerName){        for(int i=0;i<map.length;i++){            if(map[i]==playerName){                return i;            }        }        return 1;    }    /**     * 统计两位玩家谁赢了     * @param player1Name    玩家1的名字     * @param player2Name    玩家2的名字     */    private static void Results(String player1Name,String player2Name){        int p1Location=Play.findPlayer(player1Name);        int p2Location=Play.findPlayer(player2Name);        if(p1Location>p2Location){            System.err.println(p1+"的位置:"+p1Location+",恭喜"+p1+"赢了!");        }else if(p1Location<p2Location){            System.err.println(p2+"的位置:"+p2Location+",恭喜"+p2+"赢了!");        }else{            System.err.println("难分胜负啊,"+p1+"和"+p2+"打成平手,再来一局吧!");            System.out.println(p1+"的位置:"+p1Location);            System.out.println(p2+"的位置:"+p2Location);        }    }    /**     * 造成延迟假象的方法     * @param millisecond    毫秒     */    private static void working(long millisecond){        try {            Thread.sleep(millisecond);        } catch (InterruptedException e) {            e.printStackTrace();        }    }            /**     * 测试     * @param args     */    public static void main(String[] args) {        initMap("[   ]", 10);        //初始化地图        initPlayer("AAA","BBB");    //初始化玩家名称及位置,B比A要靠前一位                int p1Location=0;    //记录玩家1的位置        int p2Location=0;    //记录玩家2的位置        int whoGo=1;        //记录下一步该谁走,1为玩家1走,2为玩家2走        int nextRandom=1;    //记录下一个随机数        boolean isSuccess=false;        Scanner in=new Scanner(System.in);    //实例化控制台监听器        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~游戏开始~~~~~~~~~~~~~~~~~~~~~~~~~");    //提示文字        System.out.println("游戏规则:骰子共1(含)~10(含)个点,最先到达终点者赢。"+"\r"+"\""+p1+"\""+"首发,按y键开始掷骰子");    //提示文字        showMap();            //显示地图        System.out.println("");    //提示文字                while(in.next().equalsIgnoreCase("y")){    //如果玩家按下了y键,则继续游戏,否则推出游戏            if(map[map.length-1]==p1||map[map.length-1]==p2){    //当前任何一个玩家已经到达终点,则退出游戏                break;            }else{                System.out.println("正在掷筛子……");                nextRandom=(int)(Math.random()*10);    //下一个随机数//                Play.working(2000);                if(whoGo==1){    //玩家1走棋                    p1Location=Play.findPlayer(p1);//找出两位玩家的位置                    if(p1Location+nextRandom>map.length-1){    //如果掷到的筛子数+旧位置数超过了地图的长度                        nextRandom=map.length-p1Location-1;    //则掷到的筛子数=地图长度-当前玩家位置                        isSuccess=true;                        //掷到此筛子数的玩家一定赢了                    }                    if(nextRandom<=0){                        nextRandom+=1;                    }                    System.out.println("\""+p1+"\""+"掷到了:"+nextRandom+"点"+",正在走棋……");//                    Play.working(1000);                    if(map[p1Location]!=p2){                        map[p1Location]=mapStyle;    //将原来玩家1所在的位置替换成地图样式                    }else{                        map[p1Location]=p2;            //如果原来的位置也有玩家2,则替换成玩家2的名字                    }                    map[p1Location+nextRandom]=p1;    //随机数位置+旧位置=当前玩家所要在的位置                    whoGo=2;                }else{                    p2Location=Play.findPlayer(p2);                    if(p2Location+nextRandom>map.length-1){                        nextRandom=map.length-p2Location-1;                        isSuccess=true;                    }                    if(nextRandom<=0){                        nextRandom+=1;                    }                    System.out.println("\""+p2+"\""+"掷到了:"+nextRandom+"点"+",正在走棋...");//                    Play.working(1000);                    if(map[p2Location]!=p1){                        map[p2Location]=mapStyle;                    }else{                        map[p2Location]=p1;                    }                    map[p2Location+nextRandom]=p2;                    whoGo=1;                }                showMap();System.out.println("");    //重绘地图                if(isSuccess){                    break;                }else{                    if(whoGo==1){                        System.out.println("\""+p2+"\""+"走完了,现在轮到"+"\""+p1+"\""+"走棋,按y键开始掷骰子");                    }else{                        System.out.println("\""+p1+"\""+"走完了,现在轮到"+"\""+p2+"\""+"走棋,按y键开始掷骰子");                    }                }                continue;            }        }        System.out.println("已经决出胜负喽!下面统计战果"+"\r"+"*************************战果统计*************************");        //统计结果:        Play.Results(Play.p1,Play.p2);        return;        }}
  相关解决方案