其他分享
首页 > 其他分享> > 7-1 打球过程 (20分)

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();
		}
	}
}

标签:20,void,System,println,打球,now,过程,public,out
来源: https://blog.csdn.net/timelessx_x/article/details/111416236