使用以下代码实现从键盘上接收一个选手的联系方式 给他加分 package t8; import java.util.ArrayList; import java.util.Iterator; import t5.Player; public class ArrayListDemo { public static void main(String[] args) { ArrayList<Player> players = new ArrayList<Player>(); players.add(new Player("曾佳欣", "信息与通信", "22-1", "111")); players.add(new Player("罗曼君", "信息与通信", "22-1", "222")); players.add(new Player("张嘉颖", "信息与通信", "22-1", "333")); players.add(new Player("吴少景", "信息与通信", "22-1", "444")); System.out.println("联系方式\t姓名\t班级\t系部\t得分"); for (Player player : players) { System.out.println(player.getContact() + "\t" + player.getName() + "\t" + player.getCls() + "\t" + player.getAcademy() + "\t" + player.getScore()); } } }
时间: 2023-07-03 17:19:21 浏览: 66
可以使用如下代码实现从键盘上接收一个选手的联系方式,并给他加分:
```
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import t5.Player;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<Player> players = new ArrayList<Player>();
players.add(new Player("曾佳欣", "信息与通信", "22-1", "111"));
players.add(new Player("罗曼君", "信息与通信", "22-1", "222"));
players.add(new Player("张嘉颖", "信息与通信", "22-1", "333"));
players.add(new Player("吴少景", "信息与通信", "22-1", "444"));
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要加分的选手的联系方式:");
String contact = scanner.next();
System.out.print("请输入要加的分数:");
int score = scanner.nextInt();
Iterator<Player> iter = players.iterator();
while (iter.hasNext()) {
Player player = iter.next();
if (player.getContact().equals(contact)) {
player.setScore(player.getScore() + score);
break;
}
}
System.out.println("联系方式\t姓名\t班级\t系部\t得分");
for (Player player : players) {
System.out.println(player.getContact() + "\t" + player.getName() + "\t" +
player.getCls() + "\t" + player.getAcademy() + "\t" + player.getScore());
}
scanner.close();
}
}
```
这段代码与上一题的代码类似,只是将查找选手的条件变成了联系方式,即使用`player.getContact().equals(contact)`进行匹配。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)