我如何加班? [英] How do I add overtime?

查看:86
本文介绍了我如何加班?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是在一个篮球比赛中读取分数。我似乎无法弄清楚为什么它不会读到第四季度,它需要读到第9季所以基本上从第5季度到第9季度应该被认为是OT。另外,如何在输出分数上方添加季度标签。在此先感谢!

This is supposed to read scores in a basketabll game. I can't seem to figure out why it won't read past the 4th quarter, it needs to read up until the 9th so basically from 5th-9th quarter should be considered OT. Also, how can I add the quarter label above the output score. Thanks in advance!

public static int REGULATION_NUM_QUARTERS = 4;

public static int MAX_BOX_SCORE_LENGTH = 9;

public static final int SENTINEL = -999;

private static final int AWAY = 1;
private static final int HOME = 2; 


public static void initialize (int[] awayScores, int [] homeScores ) {
	
	for (int i = 0; i < MAX_BOX_SCORE_LENGTH; i++ ){
		awayScores [i] = SENTINEL; 

	}

	for (int i = 0; i < MAX_BOX_SCORE_LENGTH; i++ ){
		homeScores [i] = SENTINEL; 
	}

}

public static void readScores(Scanner scores, int[] awayScores, int[] homeScores) {

	int team = AWAY; 
	int quarter = 1;

	while (!gameIsOver(quarter, team, awayScores, homeScores)) {

		if (team == AWAY) { 
			awayScores [quarter -1]=  scores.nextInt();
			team = HOME; 
		}

		else {
			homeScores [quarter-1]=  scores.nextInt(); 
			team = AWAY; 
			quarter++;
		}
	}
}

public static int gameScore(int[] teamBoxScore) {
	int output = 0;
	for (int i : teamBoxScore) {
		if (i != SENTINEL) {
			output += i;
		}
	}
	return output;
}


public static int quarters(int[] teamBoxScore) {
	int output = 0;
	for (int i : teamBoxScore) {
		if (i != SENTINEL) {
			output++;
		}
	}
	return output;
}

public static boolean gameIsOver(int quarter, int team, int[] awayScores, int[] homeScores) {

	int awayScore = gameScore(awayScores);
	int homeScore = gameScore(homeScores);

	return (quarter > REGULATION_NUM_QUARTERS && team == AWAY && awayScore != homeScore)
			|| (quarter == REGULATION_NUM_QUARTERS && team == HOME && awayScore < homeScore);
}

public static void printBoxScore(int[] awayScores, int[] homeScores) {

	System.out.println();
	System.out.print(" Away: ");
	for (int v : awayScores) {
		if (v != SENTINEL) {
			System.out.print(v);
		}
	}
	System.out.println();

	System.out.print("    Home: ");
	for (int v : homeScores) {
		if (v != SENTINEL) {
			System.out.print(v);		
		}
	}			
	System.out.println();
}

public static int printBoxScore(int[] teamBoxScore) {
	int i;
	for (i = 0; i < teamBoxScore.length; i++) {
		if (teamBoxScore[i] != SENTINEL) {
			System.out.print(teamBoxScore[i] + " ");
		} else
			break;
	}
	System.out.println();
	return i;
}

public static int printBoxScore(String name, int[] teamBoxScore) {
	System.out.print(name);
	return printBoxScore(teamBoxScore);
}


public static String result(int quarter, int awayScore, int homeScore) {

	if (awayScore > homeScore) {
		return "The away team won " 
				+ awayScore + " to " + homeScore
				+ " in REGULATION";
	} 
	if (homeScore > awayScore ) {
		return "The home team won " 
				+ homeScore + " to " + awayScore
				+ " in REGULATION";
	}
	return "";
}

public static String result(int[] awayScores, int[] homeScores) {

	int awayScore = gameScore(awayScores);
	int homeScore = gameScore(homeScores);

	int quarter = printBoxScore(" Away : ", awayScores);

	printBoxScore("    Home: ", homeScores);

	return result(quarter, awayScore, homeScore);
}

public static void main(String[] args) {

	int[] awayScores = new int [MAX_BOX_SCORE_LENGTH]; 
	int[] homeScores = new int [MAX_BOX_SCORE_LENGTH];

	initialize (awayScores, homeScores);

	Scanner input = new Scanner(System.in);
	System.out.print("Do you want to see BoxScore? (y or n): ");
	String answer = input.next();

	System.out.println("Please enter the scores below");
	readScores(input, awayScores, homeScores);

	
	
	if (answer.charAt(0) == 'y' || answer.charAt(0) == 'Y') {
		System.out.println(result(awayScores, homeScores));
	} 
	else {
		System.out.println(result(quarters(awayScores), gameScore(awayScores), gameScore(homeScores)));

		
	}		
	
}
}




推荐答案

这篇关于我如何加班?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆