码迷,mamicode.com
首页 > 其他好文 > 详细

第一次迭代

时间:2016-12-22 22:55:32      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:选择   void   oid   标志位   http   img   vol   game   system.in   

package game;

import java.util.Scanner;

public class Volleyball {
    /*
     * 排球的比赛规则:全场采用五局三胜制度, 每局内: 1、赢一球得2两分 2、首先得到25分的队伍本局胜利。 3、率先赢三局的队伍全场比赛胜利。
     */
    public static void main(String[] args) {

        int[][] scoreArr = new int[5][2];// 定义二维数组,用于记录每局比赛分数
        int red = 0;// 定义红蓝变量,用于判断红蓝整场比赛输赢
        int blue = 0;

        System.out.println("--------欢迎进入排球计分系统------");
        int j = 0;
        OK: while (true) {
            System.out.println("请选择您的操作:");
            System.out.println("1、开始一局比赛  2、查询一局比分  3、退出");
            Scanner scan = new Scanner(System.in);
            String choice = scan.next();// 键入操作
            if (j < 5) {
                switch (choice) {
                case "1": {// 开始一局新比赛
                    System.out.println("第" + (j + 1) + "局比赛开始!");
                    boolean floag1 = false;
                    boolean floag2 = false;// 定义标志位,用于判断本局比赛红蓝方的输赢
                    int redScore = 0;
                    int blueScore = 0;// 定义每局比赛红蓝方基础分数
                    for (int i = 0; i < 25; i++) {
                        System.out.println("当前比分为:红方:" + redScore + "蓝方:" + blueScore);
                        System.out.println("请选择加分队伍序号:1、 蓝方 2、红方");
                        int temp = scan.nextInt();
                        if (temp == 1) {// 为蓝方加分
                            redScore += 2;// 红方分数+2分
                            if (redScore >= 25) {// 率先到达分数,则赢
                                scoreArr[j][0] = redScore;// 把红方分数写入二维数组
                                scoreArr[j][1] = blueScore;// 把蓝方分数写入二维数组
                                floag1 = true;// 判断本局比赛红方赢
                                break;
                            } else {
                                continue;
                            }
                        }
                        if (temp == 2) {
                            blueScore += 2;
                            if (redScore >= 5) {
                                scoreArr[j][0] = redScore;
                                scoreArr[j][1] = blueScore;
                                floag2 = true;
                                break;
                            } else {
                                continue;
                            }
                        }
                    }
                    System.out.println("本局比赛结束!");
                    System.out.println("比分为:红方:" + redScore + "蓝方:" + blueScore);
                    if (floag1) {// 如果蓝方floag1为true
                        red++;// 蓝方总分+1
                        System.out.println("本局结果:红方胜利");
                        if (red >= 3) {// 蓝方总分=3,蓝方赢
                            System.err.println("比赛红方胜利,全部比赛结束!请选择其他操作:");
                        }
                    }
                    if (floag2) {// 同上
                        blue++;
                        System.out.println("本局结果:蓝方胜利");
                        if (blue >= 3) {
                            System.err.println("比赛蓝方胜利,全部比赛结束!");
                        }
                    }
                    continue;
                }
                case "2":
                    System.out.println("您要查询哪一局的分数:");
                    int choiceJ = scan.nextInt();
                    // 输出二维数组对应的分数
                    if (scoreArr[choiceJ - 1][0] == 0 && scoreArr[choiceJ - 1][1] == 0) {
                        System.out.println("请先开始一局比赛");
                        break;
                    } else {
                        System.out.println("蓝方:红方-->" + scoreArr[choiceJ - 1][0] + ":" + scoreArr[choiceJ - 1][1]);
                        break;
                    }

                case "3":
                    System.out.println("退出成功,欢迎下次再来");
                    break OK;
                }
                j++;
            } else {
                System.err.println("比赛已经全部结束!请选择其他操作:");
                j++;
            }

        }

    }
}

技术分享

第一次迭代

标签:选择   void   oid   标志位   http   img   vol   game   system.in   

原文地址:http://www.cnblogs.com/zhutingting123/p/6212987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!