我已經嘗試過這個,並且無處不在尋找答案,並且似乎找不到合適的解決方法。我在這個網站上發現了很多答案,所以我已經註冊問我的問題。我有一個程序可以循環6個用戶並將它們分配給一個位置。用戶:Alpha,bravo,charlie,delta,echo和坦率。職位:1,2,3,4,5,6。我正在生成隨機數字以將用戶分配到他們的位置。我有一條規則,我正在檢查;沒有用戶可以被分配到相同的位置兩次。由於這個規則以及用戶隨機分配給電臺,有時唯一留給位置6的用戶是已經在前一輪中被分配到位置6的用戶。當發生這種情況時,我的程序崩潰了,理所當然。我如何克服這一點?我曾經有過這樣的例子,即該計劃將會做出正確的選擇,並且我有6次成功的輪換。我希望每次都有6次成功的輪換。我花了兩個星期的時間試圖弄清楚這一點。任何幫助,我可以得到將不勝感激。謝謝。我在下面粘貼了我的代碼。我正在使用Java。如果最後一個條件失敗,我該如何重新啓動一個do-while循環?
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.CardLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
public class test extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test frame = new test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
//TODO
int randnum[] = new int [7];
int numOfRotations = 1;
String[] String_CurrentOperator = new String[7];
Random rand_opers = new Random();
List<Integer> ArrayList_UsedRandNums = new ArrayList<>();
List<List<String>> ArrayList_MainOperatorHistory = new ArrayList<List<String>>();
List<String> ArrayList_AllOpers = new ArrayList<>();
List<String> ArrayList_UsedOpers = new ArrayList<>();
List<String> ArrayList_OperatorHistory1 = new ArrayList<>();
List<String> ArrayList_OperatorHistory2 = new ArrayList<>();
List<String> ArrayList_OperatorHistory3 = new ArrayList<>();
List<String> ArrayList_OperatorHistory4 = new ArrayList<>();
List<String> ArrayList_OperatorHistory5 = new ArrayList<>();
List<String> ArrayList_OperatorHistory6 = new ArrayList<>();
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(Color.DARK_GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new CardLayout(0, 0));
JButton btnShuffle = new JButton("SHUFFLE");
contentPane.add(btnShuffle, "name_253561263644851");
btnShuffle.addActionListener(this);
ArrayList_AllOpers.add("POSITION ZERO");
ArrayList_AllOpers.add("Alpha");
ArrayList_AllOpers.add("Bravo");
ArrayList_AllOpers.add("Charlie");
ArrayList_AllOpers.add("Delta");
ArrayList_AllOpers.add("Echo");
ArrayList_AllOpers.add("Frank");
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory1);
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory1);
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory2);
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory3);
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory4);
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory5);
ArrayList_MainOperatorHistory.add(ArrayList_OperatorHistory6);
}
public void actionPerformed(ActionEvent arg0) {
/*******************************
*****ASSIGN ALL OPERATORS******
*******************************/
System.out.println("THIS IS ROTATION "+numOfRotations);
genRandoms();
for(int i=1; i<6+1; i++){
do{
//randnum[i] = rand_opers.nextInt(6)+1;
String_CurrentOperator[i] = ArrayList_AllOpers.get(randnum[i]);
}
while(ArrayList_UsedOpers.contains(String_CurrentOperator[i]) || ArrayList_MainOperatorHistory.get(i).contains(String_CurrentOperator[i]));
ArrayList_UsedOpers.add(String_CurrentOperator[i]); //add to used names thus far
}
for(int i=1; i<6+1; i++){
System.out.println(String_CurrentOperator[i]); //Prints the name of the operator working on the station
ArrayList_MainOperatorHistory.get(i).add(String_CurrentOperator[i]); //adds operator to list of all users who have been assigned to this station
}
//Perform cleanup actions for next iteration of the loop
numOfRotations++; //increment the rotation count by 1
ArrayList_UsedRandNums.clear(); //clear the list of randum numbers used
ArrayList_UsedOpers.clear(); //clear the list of assigned operators
System.out.println("");
}
public void genRandoms(){
for(int i=1; i<6+1; i++){
do{
randnum[i] = rand_opers.nextInt(6)+1;
}
while (ArrayList_UsedRandNums.contains(randnum[i]));
ArrayList_UsedRandNums.add(randnum[i]); //add randnum[i] to list of random numbers used thus far
}
}
}
'Collections'中有'shuffle'兩個變種。你能用這個嗎? http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html – Brick
什麼是ArrayList_AllOpers.add(「POSITION ZERO」);'?看起來你試圖避免數組的基於零的特性。不要,你需要擁抱它。 – weston
哇,所有這些迴應已經!驚人。我會審視並嘗試並報告。非常感謝!韋斯頓你是對的。這次我故意試圖避免零基本性質。但我傾向於更多地接受它。 – theonedgo