跳水比赛 - Java 计划
Diving Competition - Java Program
我正在做一个类似于网站上另一个问题的跳水比赛程序。该程序的目标是要求用户输入潜水员的姓名、七位评委的分数以及潜水的难度等级。输入该信息后,我将根据我的用户创建的 Diver class 创建一个 Diver 对象。然后使用 insertDiver 方法,我应该按照最高的 finalScore 排在第一位的顺序将潜水员添加到潜水员数组中。我不应该对数组进行排序。使用 JOptionPane showMessageDialog 输出,按从最高 finalScore 到最低 finalScore 的顺序显示排名。因为,我将一个数组对象传递给 insertDiver 方法,该数组在该方法完成后就失效了。我尝试将 Diver[] 数组设为全局变量,但结果也不理想。所以最重要的是,我不知道如何通过最高的 finalScore 将潜水员添加到 Divers 数组中,我也不知道如何显示结果。这是我目前的代码:
import javax.swing.JOptionPane;
public class DivingCompetition
{
public static void main(String[] args)
{
String[] choices = {"Add Diver/Scores", "View Diver Standings", "Search for a diver", "Exit Program"};
String input = (String) JOptionPane.showInputDialog(null, "What do you want to do?", "Diving Competition",
JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
while(!input.equals("Exit Program"))
{
if (input.equals("Add Diver/Scores"))
addDiver();
else if (input.equals("View Diver Standings"))
viewStandings();
else if (input.equals("Search for a diver"))
searchDiver();
else
System.exit(0);
input = (String) JOptionPane.showInputDialog(null, "What do you want to do?", "Diving Competition",
JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
}
}
public static void addDiver()
{
String scoreInvalid = "The score must be between 0 and 10";
String difficultyInvalid = "The difficulty must be between 1.2 and 3.8";
double[] scores = new double[7];
int i = 0;
String tempScore = "";
String tempDifficulty = "";
double realDifficulty = 0;
double realScore = 0;
int numScores = 0;
String diverName = JOptionPane.showInputDialog("Please enter the diver's name:");
while (numScores < 7)
{
tempScore = JOptionPane.showInputDialog("Please enter the seven judge's scores one at a time");
realScore = Double.parseDouble(tempScore);
while (realScore < 0 || realScore > 10)
{
JOptionPane.showMessageDialog(null, scoreInvalid, "Invalid Score", JOptionPane.WARNING_MESSAGE);
tempScore = JOptionPane.showInputDialog("Please enter the corrected score");
realScore = Double.parseDouble(tempScore);
}
scores[i] = realScore;
i++;
numScores++;
}
tempDifficulty = JOptionPane.showInputDialog("Please enter the difficulty rating of the dive");
realDifficulty = Double.parseDouble(tempDifficulty);
while (realDifficulty < 1.2 || realDifficulty > 3.8)
{
JOptionPane.showMessageDialog(null, difficultyInvalid, "Invalid Difficulty", JOptionPane.WARNING_MESSAGE);
tempDifficulty = JOptionPane.showInputDialog("Please enter the corrected difficulty rating of the dive");
realDifficulty = Double.parseDouble(tempDifficulty);
}
Diver aDiver = new Diver(diverName, scores, realDifficulty);
Diver[] dArray = new Diver[30];
insertDiver(aDiver, dArray);
}
public static insertDiver(Diver newElement, Diver[] diverArray)
{
int addpos = 0;
int currentSize = 0;
if (diverArray[addpos] == null)
{
diverArray[addpos] = newElement;
currentSize++;
addpos++;
}
else
while (addpos < diverArray.length)
{
if (newElement.getFinalScore() > diverArray[addpos].getFinalScore())
{
diverArray[addpos] = newElement;
currentSize++;
break;
}
else
addpos++;
}
if (addpos >= diverArray.length)
diverArray[addpos] = newElement;
else
{
currentSize++;
for (int i = currentSize - 1; i > addpos; i--)
{
diverArray[i] = diverArray[i-1];
diverArray[addpos] = newElement;
}
}
for (int i = 0; i<diverArray.length; i++)
{
System.out.println(diverArray[i]);//just to see the contents of the array printed
}
}
public static void viewStandings()
{
}
public static void searchDiver()
{
}
}
这是我创建的 Diver class:
public class Diver
{//begin class
private final double MULTIPLIER = 0.6;
private String diverName;
private double[] scores = new double[7];
private double difficulty, finalScore;
//---------------------------------------------------
public Diver(String dN, double[] s, double d)
{
setDiver(dN, s, d);
}
//---------------------------------------------------
public String getName()
{
return diverName;
}
//---------------------------------------------------
public double getDifficulty()
{
return difficulty;
}
//---------------------------------------------------
public double[] getScores()
{
return scores;
}
//---------------------------------------------------
public double getFinalScore()
{
return finalScore;
}
//---------------------------------------------------
public String toString()
{
return "Diver Name: " + diverName + " Judge's Scores: " + scores +
" Dive Difficulty: " + difficulty;
}
//---------------------------------------------------
public void setDiverName(String name)
{
diverName = name.toUpperCase();
}
//---------------------------------------------------
public void setDifficulty(double diff)
{
difficulty = diff;
}
//---------------------------------------------------
public void setScores(double[] sc)
{
for(int i=0; i < 7; i++)
scores[i] = sc[i];
}
//---------------------------------------------------
public void setDiver(String nme, double[] score, double dif)
{
setDiverName(nme);
setScores(score);
setDifficulty(dif);
computeFinalScore();
}
//---------------------------------------------------
private void computeFinalScore()
{
double smallest = scores[0];
double largest = scores[0];
double scoreSum = 0;
for(int i=1; i< scores.length; i++)
{
if(scores[i] > largest)
largest = scores[i];
else if (scores[i] < smallest)
smallest = scores[i];
}
for( double i : scores)
{
scoreSum += i;
}
finalScore = (scoreSum - smallest - largest) * difficulty * MULTIPLIER;
}
//---------------------------------------------------
}//end class
程序退出,内存中没有数据可用。要使程序保持运行 运行,您必须使用 JFrame 或一般来说是使应用程序保持活动状态的线程。
将这行代码移到您的 class:
的顶部
public class DivingCompetition{
static Diver[] dArray = new Diver[30];
public static void main(String[] args){
etc
虽然这不是最好的解决方案,但它是您实施起来更容易的解决方案。正如您所说,您遇到的问题是在方法完成后数组已经死了,因为局部变量应该在其父代码完成后就死了。如果你像这样声明你的数组,你可以从你的 class.
中的任何地方访问它
要添加更多的Divers,只需在dArray中搜索一个空的地方。
最后,为了使您的代码正常工作,您需要将 insertDiver 方法更改为:
public static void insertDiver(Diver newElement, Diver[] diverArray)
为了在之后放置潜水员,而不是最后对数组进行排序,您必须在添加潜水员时实现一个简单的排序算法:
1)把第一个潜水员放在第一个位置。
2)比较第二个的得分,如果比第一个高,则将第一个车手移到数组末尾,将第二个车手放在第一位。如果没有,就把第二个潜水员放在第二个位置。
3) 随后,对于每一位新潜水员,你要做的是检查他们的分数是否高于数组中其他人的分数,以及是否将那个和所有以前得分较低的潜水员移到一个位置结束,这样他们就可以为得分比他们高的新潜水员赚取space。
f.e。我们想按以下顺序对 [12,45,34,78] 进行排序:
12 -> [null,null,null,null]
45 -> [12,null,null,null]
34 -> [45,12,null,null]
78 -> [45,34,12,null]
[78,45,34,12]
- 为 Diver 写一个比较器 Class 然后你可以使用像 TreeSet 这样的集合,这会让你的生活变得轻松一些。
- 对于显示,您可以使用 JTable。
这里是示例 运行 代码:
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class DivingCompetition
{
static DiverComparator comp = new DiverComparator();
static Set<Diver> divers = new TreeSet<Diver>(comp);
public static void main(String[] args)
{
String[] choices = {"Add Diver/Scores", "View Diver Standings", "Search for a diver", "Exit Program"};
String input = (String) JOptionPane.showInputDialog(null, "What do you want to do?", "Diving Competition",
JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
while(!input.equals("Exit Program"))
{
if (input.equals("Add Diver/Scores"))
addDiver();
else if (input.equals("View Diver Standings"))
viewStandings();
else if (input.equals("Search for a diver"))
searchDiver();
else
System.exit(0);
input = (String) JOptionPane.showInputDialog(null, "What do you want to do?", "Diving Competition",
JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
}
}
public static void addDiver()
{
String scoreInvalid = "The score must be between 0 and 10";
String difficultyInvalid = "The difficulty must be between 1.2 and 3.8";
double[] scores = new double[7];
int i = 0;
String tempScore = "";
String tempDifficulty = "";
double realDifficulty = 0;
double realScore = 0;
int numScores = 0;
String diverName = JOptionPane.showInputDialog("Please enter the diver's name:");
while (numScores < 7)
{
tempScore = JOptionPane.showInputDialog("Please enter the seven judge's scores one at a time");
realScore = Double.parseDouble(tempScore);
while (realScore < 0 || realScore > 10)
{
JOptionPane.showMessageDialog(null, scoreInvalid, "Invalid Score", JOptionPane.WARNING_MESSAGE);
tempScore = JOptionPane.showInputDialog("Please enter the corrected score");
realScore = Double.parseDouble(tempScore);
}
scores[i] = realScore;
i++;
numScores++;
}
tempDifficulty = JOptionPane.showInputDialog("Please enter the difficulty rating of the dive");
realDifficulty = Double.parseDouble(tempDifficulty);
while (realDifficulty < 1.2 || realDifficulty > 3.8)
{
JOptionPane.showMessageDialog(null, difficultyInvalid, "Invalid Difficulty", JOptionPane.WARNING_MESSAGE);
tempDifficulty = JOptionPane.showInputDialog("Please enter the corrected difficulty rating of the dive");
realDifficulty = Double.parseDouble(tempDifficulty);
}
Diver aDiver = new Diver(diverName, scores, realDifficulty);
divers.add(aDiver);
}
public static void viewStandings()
{
Object[][] rows = new Object[divers.size()][];
int i = 0;
for(Diver d : divers) {
rows[i] = new Object[3];
rows[i][0] = i+1;
rows[i][1] = d.getName();
rows[i][2] = d.getFinalScore();
i++;
}
Object[] cols = {
"Rank","Name","Final Score"
};
JTable table = new JTable(rows, cols);
JOptionPane.showMessageDialog(null, new JScrollPane(table));
}
public static void searchDiver()
{
}
}
class DiverComparator implements Comparator<Diver> {
@Override
public int compare(Diver o1, Diver o2) {
// TODO Auto-generated method stub
return (int) (o2.getFinalScore() - o1.getFinalScore());
}
}
我正在做一个类似于网站上另一个问题的跳水比赛程序。该程序的目标是要求用户输入潜水员的姓名、七位评委的分数以及潜水的难度等级。输入该信息后,我将根据我的用户创建的 Diver class 创建一个 Diver 对象。然后使用 insertDiver 方法,我应该按照最高的 finalScore 排在第一位的顺序将潜水员添加到潜水员数组中。我不应该对数组进行排序。使用 JOptionPane showMessageDialog 输出,按从最高 finalScore 到最低 finalScore 的顺序显示排名。因为,我将一个数组对象传递给 insertDiver 方法,该数组在该方法完成后就失效了。我尝试将 Diver[] 数组设为全局变量,但结果也不理想。所以最重要的是,我不知道如何通过最高的 finalScore 将潜水员添加到 Divers 数组中,我也不知道如何显示结果。这是我目前的代码:
import javax.swing.JOptionPane;
public class DivingCompetition
{
public static void main(String[] args)
{
String[] choices = {"Add Diver/Scores", "View Diver Standings", "Search for a diver", "Exit Program"};
String input = (String) JOptionPane.showInputDialog(null, "What do you want to do?", "Diving Competition",
JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
while(!input.equals("Exit Program"))
{
if (input.equals("Add Diver/Scores"))
addDiver();
else if (input.equals("View Diver Standings"))
viewStandings();
else if (input.equals("Search for a diver"))
searchDiver();
else
System.exit(0);
input = (String) JOptionPane.showInputDialog(null, "What do you want to do?", "Diving Competition",
JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
}
}
public static void addDiver()
{
String scoreInvalid = "The score must be between 0 and 10";
String difficultyInvalid = "The difficulty must be between 1.2 and 3.8";
double[] scores = new double[7];
int i = 0;
String tempScore = "";
String tempDifficulty = "";
double realDifficulty = 0;
double realScore = 0;
int numScores = 0;
String diverName = JOptionPane.showInputDialog("Please enter the diver's name:");
while (numScores < 7)
{
tempScore = JOptionPane.showInputDialog("Please enter the seven judge's scores one at a time");
realScore = Double.parseDouble(tempScore);
while (realScore < 0 || realScore > 10)
{
JOptionPane.showMessageDialog(null, scoreInvalid, "Invalid Score", JOptionPane.WARNING_MESSAGE);
tempScore = JOptionPane.showInputDialog("Please enter the corrected score");
realScore = Double.parseDouble(tempScore);
}
scores[i] = realScore;
i++;
numScores++;
}
tempDifficulty = JOptionPane.showInputDialog("Please enter the difficulty rating of the dive");
realDifficulty = Double.parseDouble(tempDifficulty);
while (realDifficulty < 1.2 || realDifficulty > 3.8)
{
JOptionPane.showMessageDialog(null, difficultyInvalid, "Invalid Difficulty", JOptionPane.WARNING_MESSAGE);
tempDifficulty = JOptionPane.showInputDialog("Please enter the corrected difficulty rating of the dive");
realDifficulty = Double.parseDouble(tempDifficulty);
}
Diver aDiver = new Diver(diverName, scores, realDifficulty);
Diver[] dArray = new Diver[30];
insertDiver(aDiver, dArray);
}
public static insertDiver(Diver newElement, Diver[] diverArray)
{
int addpos = 0;
int currentSize = 0;
if (diverArray[addpos] == null)
{
diverArray[addpos] = newElement;
currentSize++;
addpos++;
}
else
while (addpos < diverArray.length)
{
if (newElement.getFinalScore() > diverArray[addpos].getFinalScore())
{
diverArray[addpos] = newElement;
currentSize++;
break;
}
else
addpos++;
}
if (addpos >= diverArray.length)
diverArray[addpos] = newElement;
else
{
currentSize++;
for (int i = currentSize - 1; i > addpos; i--)
{
diverArray[i] = diverArray[i-1];
diverArray[addpos] = newElement;
}
}
for (int i = 0; i<diverArray.length; i++)
{
System.out.println(diverArray[i]);//just to see the contents of the array printed
}
}
public static void viewStandings()
{
}
public static void searchDiver()
{
}
}
这是我创建的 Diver class:
public class Diver
{//begin class
private final double MULTIPLIER = 0.6;
private String diverName;
private double[] scores = new double[7];
private double difficulty, finalScore;
//---------------------------------------------------
public Diver(String dN, double[] s, double d)
{
setDiver(dN, s, d);
}
//---------------------------------------------------
public String getName()
{
return diverName;
}
//---------------------------------------------------
public double getDifficulty()
{
return difficulty;
}
//---------------------------------------------------
public double[] getScores()
{
return scores;
}
//---------------------------------------------------
public double getFinalScore()
{
return finalScore;
}
//---------------------------------------------------
public String toString()
{
return "Diver Name: " + diverName + " Judge's Scores: " + scores +
" Dive Difficulty: " + difficulty;
}
//---------------------------------------------------
public void setDiverName(String name)
{
diverName = name.toUpperCase();
}
//---------------------------------------------------
public void setDifficulty(double diff)
{
difficulty = diff;
}
//---------------------------------------------------
public void setScores(double[] sc)
{
for(int i=0; i < 7; i++)
scores[i] = sc[i];
}
//---------------------------------------------------
public void setDiver(String nme, double[] score, double dif)
{
setDiverName(nme);
setScores(score);
setDifficulty(dif);
computeFinalScore();
}
//---------------------------------------------------
private void computeFinalScore()
{
double smallest = scores[0];
double largest = scores[0];
double scoreSum = 0;
for(int i=1; i< scores.length; i++)
{
if(scores[i] > largest)
largest = scores[i];
else if (scores[i] < smallest)
smallest = scores[i];
}
for( double i : scores)
{
scoreSum += i;
}
finalScore = (scoreSum - smallest - largest) * difficulty * MULTIPLIER;
}
//---------------------------------------------------
}//end class
程序退出,内存中没有数据可用。要使程序保持运行 运行,您必须使用 JFrame 或一般来说是使应用程序保持活动状态的线程。
将这行代码移到您的 class:
的顶部public class DivingCompetition{
static Diver[] dArray = new Diver[30];
public static void main(String[] args){
etc
虽然这不是最好的解决方案,但它是您实施起来更容易的解决方案。正如您所说,您遇到的问题是在方法完成后数组已经死了,因为局部变量应该在其父代码完成后就死了。如果你像这样声明你的数组,你可以从你的 class.
中的任何地方访问它要添加更多的Divers,只需在dArray中搜索一个空的地方。
最后,为了使您的代码正常工作,您需要将 insertDiver 方法更改为:
public static void insertDiver(Diver newElement, Diver[] diverArray)
为了在之后放置潜水员,而不是最后对数组进行排序,您必须在添加潜水员时实现一个简单的排序算法:
1)把第一个潜水员放在第一个位置。
2)比较第二个的得分,如果比第一个高,则将第一个车手移到数组末尾,将第二个车手放在第一位。如果没有,就把第二个潜水员放在第二个位置。
3) 随后,对于每一位新潜水员,你要做的是检查他们的分数是否高于数组中其他人的分数,以及是否将那个和所有以前得分较低的潜水员移到一个位置结束,这样他们就可以为得分比他们高的新潜水员赚取space。
f.e。我们想按以下顺序对 [12,45,34,78] 进行排序:
12 -> [null,null,null,null]
45 -> [12,null,null,null]
34 -> [45,12,null,null]
78 -> [45,34,12,null]
[78,45,34,12]
- 为 Diver 写一个比较器 Class 然后你可以使用像 TreeSet 这样的集合,这会让你的生活变得轻松一些。
- 对于显示,您可以使用 JTable。
这里是示例 运行 代码:
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class DivingCompetition
{
static DiverComparator comp = new DiverComparator();
static Set<Diver> divers = new TreeSet<Diver>(comp);
public static void main(String[] args)
{
String[] choices = {"Add Diver/Scores", "View Diver Standings", "Search for a diver", "Exit Program"};
String input = (String) JOptionPane.showInputDialog(null, "What do you want to do?", "Diving Competition",
JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
while(!input.equals("Exit Program"))
{
if (input.equals("Add Diver/Scores"))
addDiver();
else if (input.equals("View Diver Standings"))
viewStandings();
else if (input.equals("Search for a diver"))
searchDiver();
else
System.exit(0);
input = (String) JOptionPane.showInputDialog(null, "What do you want to do?", "Diving Competition",
JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
}
}
public static void addDiver()
{
String scoreInvalid = "The score must be between 0 and 10";
String difficultyInvalid = "The difficulty must be between 1.2 and 3.8";
double[] scores = new double[7];
int i = 0;
String tempScore = "";
String tempDifficulty = "";
double realDifficulty = 0;
double realScore = 0;
int numScores = 0;
String diverName = JOptionPane.showInputDialog("Please enter the diver's name:");
while (numScores < 7)
{
tempScore = JOptionPane.showInputDialog("Please enter the seven judge's scores one at a time");
realScore = Double.parseDouble(tempScore);
while (realScore < 0 || realScore > 10)
{
JOptionPane.showMessageDialog(null, scoreInvalid, "Invalid Score", JOptionPane.WARNING_MESSAGE);
tempScore = JOptionPane.showInputDialog("Please enter the corrected score");
realScore = Double.parseDouble(tempScore);
}
scores[i] = realScore;
i++;
numScores++;
}
tempDifficulty = JOptionPane.showInputDialog("Please enter the difficulty rating of the dive");
realDifficulty = Double.parseDouble(tempDifficulty);
while (realDifficulty < 1.2 || realDifficulty > 3.8)
{
JOptionPane.showMessageDialog(null, difficultyInvalid, "Invalid Difficulty", JOptionPane.WARNING_MESSAGE);
tempDifficulty = JOptionPane.showInputDialog("Please enter the corrected difficulty rating of the dive");
realDifficulty = Double.parseDouble(tempDifficulty);
}
Diver aDiver = new Diver(diverName, scores, realDifficulty);
divers.add(aDiver);
}
public static void viewStandings()
{
Object[][] rows = new Object[divers.size()][];
int i = 0;
for(Diver d : divers) {
rows[i] = new Object[3];
rows[i][0] = i+1;
rows[i][1] = d.getName();
rows[i][2] = d.getFinalScore();
i++;
}
Object[] cols = {
"Rank","Name","Final Score"
};
JTable table = new JTable(rows, cols);
JOptionPane.showMessageDialog(null, new JScrollPane(table));
}
public static void searchDiver()
{
}
}
class DiverComparator implements Comparator<Diver> {
@Override
public int compare(Diver o1, Diver o2) {
// TODO Auto-generated method stub
return (int) (o2.getFinalScore() - o1.getFinalScore());
}
}