我有一個包含8個選擇題的應用程序,每個問題的答案都存儲在2個單選按鈕中。第一個單選按鈕的值爲choice1Value
,第二個單選按鈕的值爲choice2Value
。將這些響應整理在陣列choice1ValueArray[]
和choice2ValueArray[]
中。但是,方法+getTotalRisk()
僅採用最後一個問題的值。事實上,它應該從所有問題中取得所有的值(取決於選擇哪個單選按鈕)並將它們加在一起。添加方法中的值
private int getTotalRisk() {
int totalRisk = choice1Value + choice2Value;
return totalRisk;
}
public void submitButtonClicked() {
if (infoIsValid()) {
for (int i = 1; i < choice1Array.length; i++) {
username = "Johnny";
questionID = i;
if (choice1Array[i].isSelected()) {
answer = choice1Array[i].getText();
} else if (choice2Array[i].isSelected()) {
answer = choice2Array[i].getText();
}
QuizAnswer q = new QuizAnswer(username, questionID, answer);
q.createQuizAnswer();
if (choice1Array[i].isSelected()) {
choice1Value = choice1ValueArray[i];
}
else if (choice2Array[i].isSelected()) {
choice2Value = choice2ValueArray[i];
}
System.out.println("Risk Value is " + getTotalRisk());
}
System.out.println("Your total risk value is " + getTotalRisk());
}
}
這是我輸出的結果,當點擊提交按鈕時。您可以看到它顯示了每個問題的風險值,但最終在最後時,它會計算所有風險值,但仍顯示0(您的總風險值爲0)。
connection is jdbc:odbc:myHealthDatabase
DB Query: INSERT INTO QUIZANSWER (Username, QuestionID, Answer) VALUES ('Johnny', '1', 'Above 45')
Connection is closed
Risk Value is 1
connection is jdbc:odbc:myHealthDatabase
DB Query: INSERT INTO QUIZANSWER (Username, QuestionID, Answer) VALUES ('Johnny', '2', 'Yes')
Connection is closed
Risk Value is 3
connection is jdbc:odbc:myHealthDatabase
DB Query: INSERT INTO QUIZANSWER (Username, QuestionID, Answer) VALUES ('Johnny', '3', 'Yes')
Connection is closed
Risk Value is 2
connection is jdbc:odbc:myHealthDatabase
DB Query: INSERT INTO QUIZANSWER (Username, QuestionID, Answer) VALUES ('Johnny', '4', 'Yes')
Connection is closed
Risk Value is 3
connection is jdbc:odbc:myHealthDatabase
DB Query: INSERT INTO QUIZANSWER (Username, QuestionID, Answer) VALUES ('Johnny', '5', 'No')
Connection is closed
Risk Value is 2
connection is jdbc:odbc:myHealthDatabase
DB Query: INSERT INTO QUIZANSWER (Username, QuestionID, Answer) VALUES ('Johnny', '6', 'No')
Connection is closed
Risk Value is 2
connection is jdbc:odbc:myHealthDatabase
DB Query: INSERT INTO QUIZANSWER (Username, QuestionID, Answer) VALUES ('Johnny', '7', 'Yes')
Connection is closed
Risk Value is 1
connection is jdbc:odbc:myHealthDatabase
DB Query: INSERT INTO QUIZANSWER (Username, QuestionID, Answer) VALUES ('Johnny', '8', 'Yes')
Connection is closed
Risk Value is 0
Your total risk value is 0
這裏沒有足夠的上下文來理解正在發生的事情。 getTotalRisk()是做什麼的? 'submitButtonClicked()'中聲明的所有字段在哪裏? – casablanca
我已經在getTotalRisk()方法中添加了,submitButtonClicked()是提交按鈕執行的操作,然後它將記錄所有選定的單選按鈕並計算風險值。 –