2021-10-19-学习笔记

这段代码定义了一个 Bowling 类,用于存储玩家的姓名和分数。在 main 方法中,程序接收三个玩家的数据并添加到游戏中。然后通过 getWinner() 方法找出并输出分数最高的玩家。输入示例为 Dave42、Amy103 和 Rob64,输出结果为 Amy,因为她的分数最高。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are creating a bowling game!
The given code declares a Bowling class with its constructor and addPlayer() method.
Each player of the game has a name and points, and are stored in the players HashMap.
The code in main takes 3 players data as input and adds them to the game.
You need to add a getWinner() method to the class, which calculates and outputs the name of the player with the maximum points.

Sample Input:
Dave 42
Amy 103
Rob 64

Sample Output:
Amy

import java.util.*; 

public class Bowling {
    HashMap<String, Integer> players;
    Bowling() {
        players = new HashMap<String, Integer>();
    }
    public void addPlayer(String name, int p) {
        players.put(name, p);
    }
    //code goes here
    public void getWinner() {
		String name, winner;
		int flag, points, max;
		flag = 0;
		max = 0;
		winner = "";
	   for (String i : players.keySet()) {
	   	name = i;
	   	points = players.get(i);
	   	if (flag == 0) {
	   		winner = name;
	   		max = points;
	   		flag = 1;
	   	}
	   	else {
	   		if (max < points){
	   			max = points;
	   			winner = name;
	   		}
	   	}
	   	}
	   System.out.println(winner);
	   }

    
}

public class Program {
    public static void main(String[ ] args) {
        Bowling game = new Bowling();
        Scanner sc = new Scanner(System.in);

        for(int i=0;i<3;i++) {
            String input = sc.nextLine();
            String[] values = input.split(" ");
            String name = values[0];
            int points = Integer.parseInt(values[1]);
            game.addPlayer(name, points);
        }
        game.getWinner();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萱帧闯江湖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值