2016-02-27 63 views
0

我正在研究類似於網站上另一個問題的潛水比賽項目。該計劃的目標是要求用戶輸入潛水員的名字,七位評委的分數以及潛水的難度等級。輸入信息後,我會根據用戶創建的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 
+1

你應該使用某種對潛水員的列表;更容易得多。 –

+0

Dave提供了一個掠奪: http://tutorials.jenkov.com/java-collections/collection.html –

回答

2
  1. 寫出潛水員一類比較,然後你可以使用像TreeSet的集合,這將使你的生活輕鬆一點。
  2. 對於顯示你可以使用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()); 
    } 

} 
0

程序退出,沒有數據在內存可用了。爲了讓程序繼續運行,您必須使用JFrame或共享一個使應用程序保持活動狀態的Thread。

0

移動這行代碼到類的頂部:

public class DivingCompetition{ 
    static Diver[] dArray = new Diver[30]; 

    public static void main(String[] args){ 
    etc 

儘管它不是它是一個更容易爲您實現最佳的解決方案。您所遇到的問題是,在完成該方法之後,數組已經死了,因爲在父代碼完成之後,局部變量應該會死掉。如果你像這樣聲明你的數組,你可以從你的課程中的任何地方訪問它。

要添加更多潛水員,只需在dArray中搜索空位置。

最後爲您的代碼工作,你需要改變你的insertDiver方法:

public static void insertDiver(Diver newElement, Diver[] diverArray) 

爲了把潛水員之後,沒有最終的數組排序,你必須實現一個簡單的排序算法WHILE你正在添加潛水員:

1)把第一個潛水員放在第一個位置。

2)比較第二名的潛水員分數,如果比第一名更重要,則將第一名駕駛員移動到陣列末尾一個位置,並將第二名駕駛員放在第一位。否則,將第二位潛水員放在第二位。 3)隨後,對於每個新的潛水員,你要做的是檢查他們的分數是否高於其他人的分數,並且如果是將一個以及所有先前的潛水員以較低的分數移動到朝向所以他們可以爲高分的新潛水員騰出空間,使他們的分數高於他們。

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] 
+0

我不能只將Divers添加到空位置。必須將潛水員輸入陣列,以便finalScore最高的潛水員位於第0位。進入潛水員後,我不能對陣列進行排序。 – AbhorrentScorn

+0

好的...編輯完成 – AbhorrentScorn

+0

好極了我認爲我已根據您的建議完成了該方法!非常感謝!現在數組已經按照正確的順序排列了,我只需要在JOptionPane的showMessageDialog中顯示「排名」。你能進一步擴展嗎?我知道一個循環可能需要,但只是提示如何設置? – AbhorrentScorn