我正在研究類似於網站上另一個問題的潛水比賽項目。該計劃的目標是要求用戶輸入潛水員的名字,七位評委的分數以及潛水的難度等級。輸入信息後,我會根據用戶創建的Diver類創建Diver對象。然後使用insertDiver方法,我應該按順序將潛水員添加到一系列潛水員中,這樣最高的finalScore就是第一個。我不應該排列數組。使用JOptionPane showMessageDialog輸出,按照從最高的finalScore到最低的finalScore的順序顯示排名。因爲,我正在向insertDiver方法傳遞一個數組對象,該方法完成後該數組已死亡。我試圖讓Diver []數組成爲一個全局變量,但這些結果也不好。所以底線,我不知道如何通過最高的finalScore將潛水員添加到潛水員陣列,我也不知道如何顯示結果。這裏是我的代碼至今:潛水比賽 - Java程序
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()
{
}
}
這裏是我創建的潛水員類:
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
你應該使用某種對潛水員的列表;更容易得多。 –
Dave提供了一個掠奪: http://tutorials.jenkov.com/java-collections/collection.html –