重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
class Sports {
创新互联公司2013年成立,是专业互联网技术服务公司,拥有项目成都网站设计、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元吉水做网站,已为上家服务,为吉水各地企业和个人服务,联系电话:13518219792
private String sportsName;
private int playerCount;
Sports() {
this.sportsName = "乒乓球";
this.playerCount = 4;
}
Sports(String sportsName, int playerCount) {
this.sportsName = sportsName;
this.playerCount = playerCount;
}
public String getSportsName() {
return sportsName;
}
public void setSportsName(String sportsName) {
this.sportsName = sportsName;
}
public int getPlayerCount() {
return playerCount;
}
public void setPlayerCount(int playerCount) {
this.playerCount = playerCount;
}
public void show(){
System.out.println("项目名称是:"+sportsName);
System.out.println("参加人数:"+playerCount);
}
public void minCount(Sports[] o){
int count=o[0].getPlayerCount();
String name=o[0].getSportsName();
for(int i=0;io.length;i++){
if(counto[i].getPlayerCount()){
count=o[i].getPlayerCount();
name=o[i].getSportsName();
}
}
System.out.println("所需人数最少的体育项目是:"+name);
System.out.println("所需人数是:"+count);
}
}
public class TestSports{
public static void main(String[] args) {
Sports[] sp=new Sports[4];
sp[0]=new Sports("篮球",10);
sp[1]=new Sports("足球",22);
sp[2]=new Sports("羽毛球",2);
sp[3]=new Sports();
for(int i=0;isp.length;i++){
sp[i].show();
}
new Sports().minCount(sp);
}
}
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) {
final String[] str = { "a-b 1:1", "a-c 2:1", "b-c 0:1" };
final MapString, Integer map = new HashMapString, Integer();
for (String s : str) {
final String[] temp = s.split(" ");
final String[] names = temp[0].split("-");
final String[] counts = temp[1].split(":");
for (int i = 0; i 2; i++) {
String name = names[i];
int count = Integer.parseInt(counts[i]);
if (map.containsKey(name)) {
map.put(name, map.get(name) + count);
} else {
map.put(name, count);
}
}
}
final ListTeam teams = new LinkedListTeam();
for (String name : map.keySet()) {
teams.add(new Team(name, map.get(name)));
}
Collections.sort(teams);
System.out.println("队伍 : 分");
for (Team t : teams) {
System.out.println(t.getName() + " : " + t.getCount());
}
}
}
class Team implements ComparableTeam {
private String name;
private int count;
public Team(String name, int count) {
this.name = name;
this.count = count;
}
public void addCount(int count) {
this.count = this.count + count;
}
public String getName() {
return name;
}
public int getCount() {
return count;
}
public int compareTo(Team o) {
return o.getCount() - this.count;
}
}
按你现在说的这个内容,我觉得三个类就能表现出来,我写了一下,你看看能不能满足你的要求:
/**
* 运动员信息
* @author songfeng163
*/
public class PlayerState {
private String lx; //运动类型
private String xing; // 姓
private String ming; // 名
public String getLx() {
return lx;
}
public void setLx(String lx) {
this.lx = lx;
}
public String getXing() {
return xing;
}
public void setXing(String xing) {
this.xing = xing;
}
public String getMing() {
return ming;
}
public void setMing(String ming) {
this.ming = ming;
}
//返回姓名信息
@Override
public String toString() {
return xing+ming;
}
}
/**
* 运动员得分信息
* @author songfeng163
*/
public class SoccerPlayerStats {
private PlayerState player; //运动员信息
private double spointsScored; //点球得分
private double assists; //助攻得分
private double penaltyKickRate; //罚球得分概率
public PlayerState getPlayer() {
return player;
}
public void setPlayer(PlayerState player) {
this.player = player;
}
public double getSpointsScored() {
return spointsScored;
}
public void setSpointsScored(double spointsScored) {
this.spointsScored = spointsScored;
}
public double getAssists() {
return assists;
}
public void setAssists(double assists) {
this.assists = assists;
}
public double getPenaltyKickRate() {
return penaltyKickRate;
}
public void setPenaltyKickRate(double penaltyKickRate) {
this.penaltyKickRate = penaltyKickRate;
}
//返回分数信息
@Override
public String toString() {
return "** PLAYER ADDED **"+ player.toString()+"; Points:"+ spointsScored + "; Assists: "+ assists +"; Penalty Kick %:" + penaltyKickRate;
}
}
这里是主类:
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
/**
* 主类
* @author songfeng163
*/
public class MyMain {
//添加方法
public ListSoccerPlayerStats add(ListSoccerPlayerStats list){
Scanner in=new Scanner(System.in);
PlayerState player=new PlayerState();
SoccerPlayerStats score=new SoccerPlayerStats();
System.out.println("请输入运动类型:足球,高尔夫,田径:");
player.setLx(in.next());
System.out.println("请输入姓:");
player.setXing(in.next());
System.out.println("请输入名:");
player.setMing(in.next());
System.out.println("请输入该运动员的点球得分:");
score.setSpointsScored(in.nextDouble());
System.out.println("请输入该运动员的助攻得分:");
score.setAssists(in.nextDouble());
System.out.println("请输入该运动员的罚球得分概率:");
score.setPenaltyKickRate(in.nextDouble());
score.setPlayer(player);
System.out.println(score); //显示新添加的人员
list.add(score);
return list;
}
//删除方法
public ListSoccerPlayerStats del(ListSoccerPlayerStats list){
Scanner in=new Scanner(System.in);
PlayerState player=new PlayerState();
System.out.println("请输入要删除运动员的姓:");
player.setXing(in.next());
System.out.println("请输入要删除运动员的名:");
player.setMing(in.next());
for(SoccerPlayerStats score:list){
if(score.getPlayer().getXing().equals(player.getXing())
score.getPlayer().getMing().equals(player.getMing())){
list.remove(score);
break;
}
}
return list;
}
//显示方法
public void show(ListSoccerPlayerStats list){
System.out.println("当前共有:"+list.size()+"条记录,内容如下:");
for(SoccerPlayerStats score:list){
System.out.println(score);
}
}
//保存方法
public ListSoccerPlayerStats save(ListSoccerPlayerStats list){
System.out.println("请输入要保存的文件名:");
Scanner in=new Scanner(System.in);
String fileName=in.next();
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("C:/"+fileName));
for(SoccerPlayerStats score:list){
bw.write(score.toString());
bw.newLine();
}
bw.flush();
bw.close();
}catch(Exception ex){
ex.printStackTrace();
}
System.out.println("** 1 RECORD SAVED TO "+fileName+" **");
return list;
}
public static void main(String[] args) {
MyMain myMain=new MyMain();
Scanner in=new Scanner(System.in);
//如果像你说的是数组的话,数据没有办法动态变化,所以我用了list
ListSoccerPlayerStats list=new ArrayListSoccerPlayerStats();
String cz="QUIT"; //默认的操作为退出
System.out.println("下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?");
String czin=in.next(); //输入数据
cz=czin.toUpperCase(); //转换成大写
while(!cz.equals("QUIT")){ //不是退出
if(cz.equals("ADD")){ //添加数据
list = myMain.add(list);
}else if(cz.equals("REMOVE")){ //删除数据
list = myMain.del(list);
}else if(cz.equals("SHOW")){ //显示数据
myMain.show(list);
}else if(cz.equals("SAVE")){ //保存数据
myMain.save(list);
}
System.out.println("下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?");
czin=in.next(); //输入数据
cz=czin.toUpperCase(); //转换成大写
}
System.out.println("** GOODBYE! **");
}
}
运行结果如下:
下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?
add
请输入运动类型:足球,高尔夫,田径:
足球
请输入姓:
王
请输入名:
明
请输入该运动员的点球得分:
4
请输入该运动员的助攻得分:
2
请输入该运动员的罚球得分概率:
0.25
** PLAYER ADDED **王明; Points:4.0; Assists: 2.0; Penalty Kick %:0.25
下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?
add
请输入运动类型:足球,高尔夫,田径:
高尔夫
请输入姓:
李
请输入名:
三
请输入该运动员的点球得分:
2
请输入该运动员的助攻得分:
2
请输入该运动员的罚球得分概率:
0.3
** PLAYER ADDED **李三; Points:2.0; Assists: 2.0; Penalty Kick %:0.3
下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?
show
当前共有:2条记录,内容如下:
** PLAYER ADDED **王明; Points:4.0; Assists: 2.0; Penalty Kick %:0.25
** PLAYER ADDED **李三; Points:2.0; Assists: 2.0; Penalty Kick %:0.3
下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?
save
请输入要保存的文件名:
Playerdata.txt
下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?
remove
请输入要删除运动员的姓:
李
请输入要删除运动员的名:
三
下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?
show
当前共有:1条记录,内容如下:
** PLAYER ADDED **王明; Points:4.0; Assists: 2.0; Penalty Kick %:0.25
下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?
quit
** GOODBYE! **