@Override public Result itemScore(Score score) { //可获取运动员姓名 分组 List<Score> list = scoreMapper.itemsUsername(score); //可获取运动员成绩 时间 List<Score> allList = scoreMapper.itemsScore(score); ArrayList<Object> series = new ArrayList<>(); ArrayList<Date> dates = new ArrayList<>(); //获取xAxis for (Score a:allList) { if(dates.size()==0){ dates.add(a.getMatchTime()); }else{ System.out.println(); if(dates.indexOf(a.getMatchTime())<0){ dates.add(a.getMatchTime()); } } } //获取series数组 for (Score s:list) { HashMap<String, Object> map = new HashMap<>(); map.put("name",s.getUsername()); map.put("type","line"); List<BigDecimal> data = allList.stream().filter(x -> x.getUsername().equals(s.getUsername())).map((item) -> { return item.getMatchScore(); } ).collect(Collectors.toList()); map.put("data",data); series.add(map); } HashMap<Object, Object> map = new HashMap<>(); map.put("series",series); map.put("xAxis",dates); return Result.ok(map); }
时间: 2024-01-28 19:03:14 浏览: 68
这段代码看起来像是 Java 语言编写的,它是用来处理运动员比赛成绩的逻辑。它首先从数据库中获取分组和成绩信息,然后根据成绩信息生成 x 轴的日期数组和 series 数组,其中 series 数组中包含每个运动员的成绩信息。最后,它将这些数组打包成一个 HashMap,作为返回结果。具体的逻辑我不太清楚,因为我不知道 ScoreMapper 的具体实现和数据库结构,但是这段代码的目的应该是为了展示每个运动员在比赛中的成绩变化趋势。
相关问题
{ name: sex: age: subject: score: } 当前有一个数组array里以上面的结构存放了一些数据, 用java实现以下需求 1.返回每个科目分数都在90以上的所有女同学的数组,并根据语文分数和同学姓名排序
1.首先需要遍历整个数组,找到所有女同学且分数都在90以上的数据。可以使用for循环和if语句实现:
```java
List<Map<String, Object>> result = new ArrayList<>(); // 用List存储结果
for (Map<String, Object> data : array) {
if (data.get("sex").equals("女") && (int) data.get("score") >= 90) {
result.add(data);
}
}
```
2.根据语文分数和同学姓名排序,可以使用Collections.sort()方法和Comparator接口实现:
```java
Collections.sort(result, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
int score1 = (int) o1.get("score");
int score2 = (int) o2.get("score");
if (score1 != score2) {
return score2 - score1; // 分数从高到低排序
} else {
return ((String) o1.get("name")).compareTo((String) o2.get("name")); // 分数相同按姓名升序排序
}
}
});
```
完整代码:
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Map<String, Object>> array = new ArrayList<>();
Map<String, Object> data1 = new HashMap<>();
data1.put("name", "张三");
data1.put("sex", "男");
data1.put("age", 18);
data1.put("subject", "语文");
data1.put("score", 95);
array.add(data1);
Map<String, Object> data2 = new HashMap<>();
data2.put("name", "李四");
data2.put("sex", "女");
data2.put("age", 17);
data2.put("subject", "数学");
data2.put("score", 92);
array.add(data2);
Map<String, Object> data3 = new HashMap<>();
data3.put("name", "王五");
data3.put("sex", "女");
data3.put("age", 16);
data3.put("subject", "英语");
data3.put("score", 89);
array.add(data3);
Map<String, Object> data4 = new HashMap<>();
data4.put("name", "赵六");
data4.put("sex", "女");
data4.put("age", 18);
data4.put("subject", "语文");
data4.put("score", 98);
array.add(data4);
List<Map<String, Object>> result = new ArrayList<>(); // 用List存储结果
for (Map<String, Object> data : array) {
if (data.get("sex").equals("女") && (int) data.get("score") >= 90) {
result.add(data);
}
}
Collections.sort(result, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
int score1 = (int) o1.get("score");
int score2 = (int) o2.get("score");
if (score1 != score2) {
return score2 - score1; // 分数从高到低排序
} else {
return ((String) o1.get("name")).compareTo((String) o2.get("name")); // 分数相同按姓名升序排序
}
}
});
System.out.println(result);
}
}
```
输出结果:
```
[{name=赵六, sex=女, age=18, subject=语文, score=98}, {name=张三, sex=女, age=17, subject=数学, score=92}]
```
● A menu including Open and Exit where Open starts a JFileChooser to select the file with the questions inside and Exit ends the programme. ● Once a file is loaded, the GUI should display one question and its answers at a time. ● The user should be able to select an answer and they should be informed if they were correct or not. ● The user should be made aware of the number of correctly answered and the total number of questions answered. ● The user should only be able to proceed to the next question once they answered the current one. ● Once all questions have been answered, the user should be informed of their overall score and that the game has finished. The Open menu item should now be enabled to start a new quiz. Optionally, you can add a restart menu item to redo the current quiz.
下面是一个实现上述功能的Java Swing程序:
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
public class QuizGame implements ActionListener {
private JFrame frame;
private JLabel questionLabel;
private List<Question> questions;
private int currentQuestionIndex;
private int numCorrect;
private int numAnswered;
private JRadioButton[] answerButtons;
private JButton nextButton;
private JButton restartButton;
private JFileChooser fileChooser;
public QuizGame() {
frame = new JFrame("Quiz Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
JPanel questionPanel = new JPanel();
questionPanel.setLayout(new BorderLayout());
questionLabel = new JLabel();
questionLabel.setHorizontalAlignment(SwingConstants.CENTER);
questionPanel.add(questionLabel, BorderLayout.CENTER);
JPanel answerPanel = new JPanel();
answerPanel.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
nextButton = new JButton("Next");
nextButton.addActionListener(this);
buttonPanel.add(nextButton);
restartButton = new JButton("Restart");
restartButton.addActionListener(this);
buttonPanel.add(restartButton);
answerPanel.add(buttonPanel, BorderLayout.SOUTH);
answerButtons = new JRadioButton[4];
for (int i = 0; i < answerButtons.length; i++) {
answerButtons[i] = new JRadioButton();
answerPanel.add(answerButtons[i], BorderLayout.NORTH);
}
frame.add(questionPanel, BorderLayout.CENTER);
frame.add(answerPanel, BorderLayout.SOUTH);
fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
questions = new ArrayList<Question>();
currentQuestionIndex = -1;
numCorrect = 0;
numAnswered = 0;
setMenu();
setNextEnabled(false);
setFrameVisible(true);
}
private void setMenu() {
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu("File");
MenuItem openItem = new MenuItem("Open");
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(";");
String question = parts[0];
String[] answers = parts[1].split(",");
String correctAnswer = answers[0];
Question q = new Question(question, answers, correctAnswer);
questions.add(q);
}
reader.close();
Collections.shuffle(questions);
currentQuestionIndex = -1;
numCorrect = 0;
numAnswered = 0;
setNextEnabled(true);
nextQuestion();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(openItem);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
frame.setMenuBar(menuBar);
}
private void setFrameVisible(boolean visible) {
frame.setVisible(visible);
}
private void setNextEnabled(boolean enabled) {
nextButton.setEnabled(enabled);
}
private void setRestartEnabled(boolean enabled) {
restartButton.setEnabled(enabled);
}
private void nextQuestion() {
currentQuestionIndex++;
if (currentQuestionIndex < questions.size()) {
Question q = questions.get(currentQuestionIndex);
questionLabel.setText(q.getQuestion());
String[] answers = q.getAnswers();
Collections.shuffle(Arrays.asList(answers));
for (int i = 0; i < answerButtons.length; i++) {
answerButtons[i].setText(answers[i]);
answerButtons[i].setSelected(false);
}
} else {
double score = (double) numCorrect / numAnswered;
String message = "You answered " + numCorrect + " out of " + numAnswered + " questions correctly.\n";
if (score >= 0.9) {
message += "Excellent!";
} else if (score >= 0.7) {
message += "Good job!";
} else if (score >= 0.5) {
message += "Not bad.";
} else {
message += "You need more practice.";
}
questionLabel.setText(message);
setNextEnabled(false);
setRestartEnabled(true);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == nextButton) {
if (currentQuestionIndex >= 0) {
Question q = questions.get(currentQuestionIndex);
numAnswered++;
boolean correct = false;
for (int i = 0; i < answerButtons.length; i++) {
JRadioButton button = answerButtons[i];
if (button.isSelected()) {
String answer = button.getText();
if (answer.equals(q.getCorrectAnswer())) {
correct = true;
numCorrect++;
}
break;
}
}
String message = correct ? "Correct!" : "Incorrect!";
questionLabel.setText(message);
}
setNextEnabled(false);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
nextQuestion();
}
});
} else if (e.getSource() == restartButton) {
currentQuestionIndex = -1;
numCorrect = 0;
numAnswered = 0;
setRestartEnabled(false);
setNextEnabled(true);
nextQuestion();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
QuizGame game = new QuizGame();
}
});
}
private class Question {
private String question;
private String[] answers;
private String correctAnswer;
public Question(String question, String[] answers, String correctAnswer) {
this.question = question;
this.answers = answers;
this.correctAnswer = correctAnswer;
}
public String getQuestion() {
return question;
}
public String[] getAnswers() {
return answers;
}
public String getCorrectAnswer() {
return correctAnswer;
}
}
}
```
这个程序使用了Java Swing的JFrame、JLabel、JRadioButton、JButton和JFileChooser来实现一个简单的问答游戏。在菜单中,使用JFileChooser选择包含问题和答案的文件,并将它们加载到List<Question>中。在游戏过程中,使用currentQuestionIndex跟踪当前问题的索引,并使用numCorrect和numAnswered跟踪正确回答的问题的数量和总问题数量。使用JRadioButton显示答案选项,并在用户选择一个选项后检查它是否正确,并显示相应的消息。使用nextButton和restartButton在用户回答问题后控制游戏的流程。最后,使用JOptionPane显示用户的总体分数和游戏结束的消息。
阅读全文