2012-01-24 44 views
0

我有一個包含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 
+1

這裏沒有足夠的上下文來理解正在發生的事情。 getTotalRisk()是做什麼的? 'submitButtonClicked()'中聲明的所有字段在哪裏? – casablanca

+0

我已經在getTotalRisk()方法中添加了,submitButtonClicked()是提交按鈕執行的操作,然後它將記錄所有選定的單選按鈕並計算風險值。 –

回答

1

陣列從零開始

for (int i = 1; i < rArray.length; i++) 

也許應該

for (int i = 0; i < rArray.length; i++) 
0

你沒有積累的結果(我覺得)你想,你繼續覆蓋answerchoice1Valuechoice2Value每次通過for循環。這就是爲什麼你從最後一個問題獲得「價值」。

但很難在不知道更多的情況下更詳細地回答您的問題。正如卡薩布蘭卡在評論中所說,重要的信息在這裏不復存在。你想做什麼?什麼是answer以及它是如何使用的?什麼是你的g/v/r/fArray變量(你應該給他們更好的名字)?你能舉一個你的單選按鈕應該加起來的例子嗎?

您的評論後編輯:我覺得需要像這樣加起來你的風險值:

public int getTotalRisk() { 
    if (!infoIsValid()) { 
    return 0; 
    } 
    int totalRisk = 0; 
    // assuming choice1Array.length == choice2Array.length...? 
    for (int i = 0; i < choice1Array.length; i++) { 
    if (choice1Array[i].isSelected()) { 
     answer = choice1Array[i].getText(); 
    } else if (choice2Array[i].isSelected()) { 
     answer = choice2Array[i].getText(); 
    } // is it possible for neither to be selected? 
    QuizAnswer quizAnswer = new QuizAnswer("Johnny", i, answer); 
    quizAnswer.createQuizAnswer(); // not sure what this line is meant to do 
    if (choice1Array[i].isSelected()) { 
     totalRisk += choice1ValueArray[i]; 
    } else if (choice2Array[i].isSelected()) { 
     totalRisk += choice2ValueArray[i]; 
    } // if one is always selected, make it if/else, not if/else if 
    } 
    return totalRisk; 
} 

我冒昧提出一些修改,隨意使用或忽略他們......重點是,爲了獲得總體風險,您需要每次通過for循環時加入一個變量(totalRisk),以便每個值i(和I已經使循環從0開始,即數組中的第一個位置,正如Peter的答案所示;如果有一個有效的循環從1開始的理由,例如a t在陣列中的第二個位置)。正如前面提到的,你正在做的是每次在循環中覆蓋「總」值,所以最後你得到了最後一個循環的值。

由於我不知道createQuizAnswer()方法是幹什麼的,所以我會讓你把它用在你的代碼中,但希望你能明白你做錯了什麼。作爲最終的一般性建議,您應該閱讀變量作用域,因爲我認爲您並不總是知道如何使用它們。

+0

我已經編輯了數組名稱,以便按照您的要求更好地理解。基本上,我的每個單選按鈕都分配有值。因此,當選擇一個單選按鈕時,分配的值將從該特定的所選單選按鈕中獲取。到最後時,8個單選按鈕將被選中,因爲我有8個問題。因此,我想添加每個單選按鈕的指定值並顯示總值。 –