7-1 打球过程 (20分)

java旧题复习
7-1 打球过程 (20分)
利用模板方法来构造相关类实现下述过程: 各种球类的玩法虽然不同,但是球类比赛的过程是类似的,都包含如下几个步骤: 1球员报道–>2比赛开始–>3比赛–>4比赛结束–>5公布比赛成绩,且其中1 2 4步相同 第3步根据球类不同,玩法不同,第5步根据得分不同,公布方式结果不同 构造类BallMatch表示球类比赛,包含方法compete表示真个比赛过程 构造各个比赛过程的函数checkin,start,play,end,annouceResult 打印信息如下: now checking in now starting now playing football now ending now annoucing result: 2-3 构造类FootballMatch和BasketBallMatch,实现具体的比赛过程。

在main函数中,读入整数i,如果为1,则构造一个足球比赛过程,如果为2则构造一个篮球比赛过程 打印比赛过程

输入格式:
比赛类型 比分

输出格式:
比赛过程信息

输入样例:
在这里给出一组输入。例如:

1 2-3

输出样例:
在这里给出相应的输出。例如:

now checking in
now starting
now playing football
now ending
now annoucing result: 2-3

答案1:
(考试时如果没有时间了这么写吧。。)

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		
		int i=in.nextInt();
		String score=in.next();
		if(i==1) {
			System.out.println("now checking in");
			System.out.println("now starting");
			System.out.println("now playing football");
			System.out.println("now ending");
			System.out.println("now annoucing result: "+score);
		}else if(i==2) {
			System.out.println("now checking in");
			System.out.println("now starting");
			System.out.println("now playing basketball");
			System.out.println("now ending");
			System.out.println("now annoucing result: "+score);	
		}
	}
}

答案2:

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		
		int i=in.nextInt();
		BallMatch test=new BallMatch();
		if(i==1) {
			FootBallMatch f=new FootBallMatch(in.next());
			test.compete(f);
		}else if(i==2) {
			BasketBallMatch f=new BasketBallMatch(in.next());
			test.compete(f);
		}
	}
}

class FootBallMatch{
	String score;
	
	public FootBallMatch(String s) {
		this.score=s;
	}
	public void checking () {
		System.out.println("now checking in");
	}
	
	public void starting() {
		System.out.println("now starting");
	}
	
	public void playing () {
		System.out.println("now playing football");
	}
	
	public void ending() {
		System.out.println("now ending");
	}
	
	public void annoucing () {
		System.out.println("now annoucing result: "+this.score);
	}
}
class BasketBallMatch{
	String score;
	
	public BasketBallMatch(String s) {
		this.score=s;
	}
	public void checking () {
		System.out.println("now checking in");
	}
	
	public void starting() {
		System.out.println("now starting");
	}
	
	public void playing () {
		System.out.println("now playing basketball");
	}
	
	public void ending() {
		System.out.println("now ending");
	}
	
	public void annoucing () {
		System.out.println("now annoucing result: "+this.score);
	}
}

class BallMatch{
	public static void compete(Object o) {
		if(o instanceof FootBallMatch) {
			FootBallMatch f=(FootBallMatch)o;
			f.checking();
			f.starting();
			f.playing();
			f.ending();
			f.annoucing();
		}else if(o instanceof BasketBallMatch) {
			BasketBallMatch f=(BasketBallMatch)o;
			f.checking();
			f.starting();
			f.playing();
			f.ending();
			f.annoucing();
		}
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用 Python 语言实现的打球球小游戏示例代码: ```python import pygame import random # 初始化 Pygame pygame.init() # 游戏窗口大小 WINDOW_WIDTH = 800 WINDOW_HEIGHT = 600 # 创建游戏窗口 screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption("打球球小游戏") # 设置游戏帧率 clock = pygame.time.Clock() # 球的大小和速度 BALL_RADIUS = 20 BALL_SPEED = 5 # 球的初始位置和速度 ball_x = random.randint(BALL_RADIUS, WINDOW_WIDTH - BALL_RADIUS) ball_y = random.randint(BALL_RADIUS, WINDOW_HEIGHT - BALL_RADIUS) ball_speed_x = BALL_SPEED ball_speed_y = BALL_SPEED # 玩家的位置和大小 player_width = 100 player_height = 20 player_x = (WINDOW_WIDTH - player_width) // 2 player_y = WINDOW_HEIGHT - player_height # 游戏数 score = 0 # 游戏结束标志 game_over = False # 加载字体 font = pygame.font.SysFont(None, 36) # 游戏循环 while not game_over: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player_x -= 10 elif event.key == pygame.K_RIGHT: player_x += 10 # 移动球 ball_x += ball_speed_x ball_y += ball_speed_y # 球碰到左右边界反弹 if ball_x - BALL_RADIUS < 0 or ball_x + BALL_RADIUS > WINDOW_WIDTH: ball_speed_x = -ball_speed_x # 球碰到顶部反弹 if ball_y - BALL_RADIUS < 0: ball_speed_y = -ball_speed_y # 球碰到玩家反弹 if ball_y + BALL_RADIUS >= player_y and \ ball_x >= player_x and \ ball_x <= player_x + player_width: ball_speed_y = -ball_speed_y score += 10 # 球落下游戏结束 if ball_y + BALL_RADIUS > WINDOW_HEIGHT: game_over = True # 绘制游戏界面 screen.fill((255, 255, 255)) pygame.draw.circle(screen, (255, 0, 0), (ball_x, ball_y), BALL_RADIUS) pygame.draw.rect(screen, (0, 0, 255), (player_x, player_y, player_width, player_height)) score_text = font.render("Score: " + str(score), True, (0, 0, 0)) screen.blit(score_text, (10, 10)) pygame.display.update() # 控制游戏帧率 clock.tick(60) # 游戏结束,退出 Pygame pygame.quit() ``` 你可以将这段代码保存到一个 Python 文件中并运行,就可以玩到这个打球球小游戏了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值