模拟打保龄球

题目要求如图

下面就是写的代码

package mt;

public class Bowling {
	public static void main(String[] args) {
		int[] score_1 = new int[10];  //第i轮的第一球得分
		int[] score_2 = new int[10];  //第i轮的第二球得分
		int sumScore =0;  //累计得分
		int one;    //第一次击倒数
		int two;    //第二次击倒数
		int[][] x = new int[11][4];	//记录打球过程
		int[] index1 = new int[10] ;  //记录第1次就打倒10个瓶子的下标
		int[] index2 =new int[10] ;   //记录补中10个瓶子的下标
		for (int i = 0; i < 10; i++) {
			one = (int) (Math.random() * 11);  //模拟每局的第一次击球
			two = (int) (Math.random() * (10 - one + 1));  //模拟每局的第二次击球
			x[i][0] = one;
			x[i][1] = two;
			if (one == 10) {
				score_1[i] = 10;
				score_2[i] = 0;
				index1[i] = i+1;
			}
			else if (one + two == 10) {
				score_1[i] = one;
				score_2[i] = two;
				index2[i] = i+1;
			}
			else {
				score_1[i] = one;
				score_2[i] = two;
			}
			x[i][2] = score_1[i] + score_2[i];
		}


		for (int i = 0; i < 10-1; i++) {
			if (index1[i] != 0) {
				x[i][2] = score_1[i] + score_1[i+1] + score_2[i+1];
			}
			if (index2[i] != 0) {
				x[i][2] += score_1[i+1];
			}
		}
		System.out.println("\t轮数" + "\t第一次击倒瓶数" + "\t第二次击倒瓶数" + "\t本轮得分" + "\t累计得分");
		
		for (int i = 0; i < 10+1 ; i++) {
			x[i][3] = sumScore += x[i][2];
			if (index1[9] == 10 || index2[9] == 10) {
				one = (int) (Math.random() * 11);
				two = (int) (Math.random() * (10 - one + 1));
				x[10][0] = one;
				x[10][1] = two;
				x[10][2] = one + two;
				x[10][3] = x[9][3] + x[10][2];	 
			}
			int a = i+1;
			System.out.println( "\t" + a + "\t\t" + x[i][0]  + "\t\t" + x[i][1]
					+ "\t" + x[i][2] + "\t" + x[i][3] );
		}

		

	}
}

posted @ 2015-05-10 23:47  乜天  阅读(127)  评论(0编辑  收藏  举报