-1
好了,通過我的大部分問題(在Java中...今天...)ANOTHER IndexOutofBounds例外
在這個GUI問卷得到的,我設法步我的問題,讓我的價值觀,而是爲了由於某種原因,它在循環的每次迭代期間都會拋出一個超出界限的異常......它實際上並沒有導致程序結束或其他任何事情,而是要求我翻閱問題並給出響應,我看到我的控制檯顯示出界限直到最後一個問題被詢問時,循環的每個計數都會出現錯誤。 如果我註釋掉我的calcPersonnelRisk(),它在索引計數器是列表大小時運行,它會在沒有任何錯誤的情況下運行。困惑,因爲該方法只是從我的答案中賦值給我的變量,而不明白爲什麼有這樣的代碼會搞砸它。作爲一名程序員,我仍然處於初級階段,並且仍然在發展我的高效調試能力......但我們首先感謝任何輸入。提前致謝!
public class ORMRiskCalculator extends JFrame{
private JButton yesButton, noButton, exitButton, enterButton;
private JLabel message;
private JTextField textField;
private JFrame frmOrmRiskCalculator;
private final ButtonGroup buttonGroup = new ButtonGroup();
private int questionIndex;
private ArrayList<String> questions = new ArrayList<String>();
private ArrayList<Integer> responses = new ArrayList<Integer>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ORMRiskCalculator window = new ORMRiskCalculator();
window.frmOrmRiskCalculator.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ORMRiskCalculator() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmOrmRiskCalculator = new JFrame();
frmOrmRiskCalculator.getContentPane().setBackground(new Color(0, 102, 153));
frmOrmRiskCalculator.setTitle("ORM Risk Calculator");
frmOrmRiskCalculator.setBounds(100, 100, 650, 500);
frmOrmRiskCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmOrmRiskCalculator.getContentPane().setLayout(null);
message = new JLabel("Create a new ORM Report?");
message.setVerticalAlignment(SwingConstants.CENTER);
message.setHorizontalAlignment(SwingConstants.CENTER);
message.setFont(new Font("Tahoma", Font.BOLD, 20));
message.setBounds(10, 10, 614, 109);
frmOrmRiskCalculator.getContentPane().add(message);
yesButton = new JButton("YES");
yesButton.setFont(new Font("Tahoma", Font.BOLD, 14));
yesButton.setForeground(Color.BLACK);
yesButton.setBackground(Color.GREEN);
yesButton.setBounds(80, 240, 200, 50);
frmOrmRiskCalculator.getContentPane().add(yesButton);
yesButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
questions.clear();
responses.clear();
getAnswers();
}
});
noButton = new JButton("NO");
noButton.setBackground(Color.RED);
noButton.setFont(new Font("Tahoma", Font.BOLD, 14));
noButton.setBounds(80, 330, 200, 50);
frmOrmRiskCalculator.getContentPane().add(noButton);
noButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
textField = new JTextField();
textField.setBounds(350, 275, 100, 50);
textField.setFont(new Font("Tahoma", Font.BOLD, 20));
frmOrmRiskCalculator.getContentPane().add(textField);
textField.setColumns(10);
enterButton = new JButton("Enter");
enterButton.setFont(new Font("Tahoma", Font.BOLD, 14));
enterButton.setBounds(450, 275, 100, 50);
buttonGroup.add(enterButton);
frmOrmRiskCalculator.getContentPane().add(enterButton);
exitButton = new JButton("EXIT");
exitButton.setBackground(Color.GRAY);
exitButton.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
exitButton.setBounds(80, 425, 470, 25);
frmOrmRiskCalculator.getContentPane().add(exitButton);
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shutDown();
}
});
}
private void setupQuestions(){
questions.add("How many mission personnel had less than normal rest?");
questions.add("How many mission personnel have an illness that may impact the mission?");
questions.add("How many mission personnel are taking medications that may affect duty?");
questions.add("How many mission personnel are scheduled to work longer than ten hours?");
questions.add("How many mission personnel worked greater than ten hours on the previous shift?");
questions.add("How many mission personnel swapped from night shift to day shift in the last 24 hrs?");
questions.add("How many mission personnel are still in a training status or are a TEMP RCOs)?");
questions.add("How many mission personnel are on duty during flight operations?");
}
protected void getAnswers() {
setupQuestions();
yesButton.setVisible(false);
noButton.setVisible(false);
message.setFont(new Font("Tahoma", Font.BOLD, 14));
message.setText(questions.get(questionIndex++));
enterButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int r= Integer.parseInt(textField.getText().trim());
responses.add(r);
textField.setText("");
if(questionIndex < questions.size())
message.setText(questions.get(questionIndex++));
else {message.setText("Done!"); calcPersonnelRisk();}
//do finishing stuff, move to the next method!
}});
}//end getAnswers
protected void calcPersonnelRisk() {
int lessThanNormalRest, illness, meds, lengthOfDay, previousDutyDay, crewExperience,
shiftSwap, minManning;
//probably better in this instance to use TreeMaps for future iterations of this program?
lessThanNormalRest = responses.get(0);
illness = responses.get(1);
meds = responses.get(2);
lengthOfDay=responses.get(3);
previousDutyDay=responses.get(4);
shiftSwap=responses.get(5);
crewExperience = responses.get(6);
if(responses.get(7) >= 4)minManning = 2;
else minManning=0;
PersonnelRisk pRisk = new PersonnelRisk(lessThanNormalRest,illness,meds,lengthOfDay,
previousDutyDay,crewExperience,shiftSwap,minManning);
message.setFont(new Font("Tahoma", Font.BOLD, 20));
message.setText("Your calculated personnel Risk is: "+pRisk.calcRisk()
+"\n\t Run again?");
yesButton.setVisible(true);
noButton.setVisible(true);
}
private void shutDown(){
message.setFont(new Font("Tahoma", Font.BOLD, 26));
message.setText("Good bye!");//why doesn't this work? Sleeps, and closes,
//doesn't show my text message.
try {
Thread.sleep(1000);//also tried a wait(), notify(), but it seemed like the notify never allowed it to get to the exit command
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.exit(0);
}//end shutDown method
}//endORMRiskCalculator
如果用戶想要運行另一個計算,我在收到錯誤後添加了清除功能。目前它只設置爲允許用戶點擊yes按鈕來啓動問題,然後它不再可見 – Ted
我明白你的意思了!是的,讓主創建數組而不是在getAnswers方法中創建數組,肯定會造成一些問題......修復它!現在它不會把錯誤拋到最後。 (在我完成整個循環之後) – Ted
很高興幫助。這個問題關閉了嗎?因爲我們沒有新的代碼很難說出發生了什麼 –