【发布时间】:2017-10-07 15:28:55
【问题描述】:
我正在制作一个简单的测验应用程序,但我无法正确设置评分逻辑。 我有 5 个问题,每个问题有 6 个复选框,每个问题在 6 个复选框中都有 2 个正确答案。我想让评分逻辑将答案标记为正确:(类似这样:)
if (ALL_RIGHT_CHOICES_ARE_CHECKED && ALL_OTHERS_ARE_NOT_CHECKED) {
+=1}
不限制复选框选择(因为我还没有得到这个概念)。我该如何实现?这是我当前的代码:
package com.example.android.architecturequizapp;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//method called when Submit&Grade button is clicked
public void submitAnswers(View view) {
//EditText name
EditText nameTypeTextBox = (EditText) findViewById(R.id.name_edit_text);
String typeName = nameTypeTextBox.getText().toString();
//Question 1: Name Question: Eiffel Tower
RadioButton q1Name = (RadioButton) findViewById(R.id.q1_eiffel_tower_radio_button);
boolean hasQ1NameCorrect = q1Name.isChecked();
//Question 1: Location Question: Paris
CheckBox q1Location1 = (CheckBox) findViewById(R.id.q1_paris_checkbox);
boolean hasQ1Location1Correct = q1Location1.isChecked();
//Question 1: Location Question: France
CheckBox q1Location2 = (CheckBox) findViewById(R.id.q1_france_checkbox);
boolean hasQ1Location2Correct = q1Location2.isChecked();
//Question 2: Name Question: Colosseum
RadioButton q2Name = (RadioButton) findViewById(R.id.q2_colosseum_radio_button);
boolean hasQ2NameCorrect = q2Name.isChecked();
//Question 2: Location Question: Rome
CheckBox q2Location1 = (CheckBox) findViewById(R.id.q2_rome_checkbox);
boolean hasQ2Location1Correct = q2Location1.isChecked();
//Question 2: Location Question: Italy
CheckBox q2Location2 = (CheckBox) findViewById(R.id.q2_italy_checkbox);
boolean hasQ2Location2Correct = q2Location2.isChecked();
//Question 3: Name Question: Tower of Pisa
RadioButton q3Name = (RadioButton) findViewById(R.id.q3_tower_of_pisa_radio_button);
boolean hasQ3NameCorrect = q3Name.isChecked();
//Question 3: Location Question: Pisa
CheckBox q3Location1 = (CheckBox) findViewById(R.id.q3_pisa_checkbox);
boolean hasQ3Location1Correct = q3Location1.isChecked();
//Question 3: Location Question: Italy
CheckBox q3Location2 = (CheckBox) findViewById(R.id.q3_italy_checkbox);
boolean hasQ3Location2Correct = q3Location2.isChecked();
//Question 4: Name Question: Casa Batllo
RadioButton q4Name = (RadioButton) findViewById(R.id.q4_casa_batllo_radio_button);
boolean hasQ4NameCorrect = q4Name.isChecked();
//Question 4: Location Question: Barcelona
CheckBox q4Location1 = (CheckBox) findViewById(R.id.q4_barcelona_checkbox);
boolean hasQ4Location1Correct = q4Location1.isChecked();
//Question 4: Location Question: Spain
CheckBox q4Location2 = (CheckBox) findViewById(R.id.q4_spain_checkbox);
boolean hasQ4Location2Correct = q4Location2.isChecked();
//Question 5: Name Question: Eiffel Tower
RadioButton q5Name = (RadioButton) findViewById(R.id.q5_opera_house_radio_button);
boolean hasQ5NameCorrect = q5Name.isChecked();
//Question 5: Location Question: Sydney
CheckBox q5Location1 = (CheckBox) findViewById(R.id.q5_sydney_checkbox);
boolean hasQ5Location1Correct = q5Location1.isChecked();
//Question 5: Location Question: Australia
CheckBox q5Location2 = (CheckBox) findViewById(R.id.q5_australia_checkbox);
boolean hasQ5Location2Correct = q5Location2.isChecked();
int questionsCorrect = calculateRightAnswers(hasQ1NameCorrect, hasQ1Location1Correct, hasQ1Location2Correct, hasQ2NameCorrect, hasQ2Location1Correct, hasQ2Location2Correct, hasQ3NameCorrect, hasQ3Location1Correct, hasQ3Location2Correct, hasQ4NameCorrect, hasQ4Location1Correct, hasQ4Location2Correct, hasQ5NameCorrect, hasQ5Location1Correct, hasQ5Location2Correct);
//toast message for questions correct/score
//less than 5 = poor results
if (questionsCorrect <= 5) {
Context context = getApplicationContext();
CharSequence text = "Poor results! You scored " + questionsCorrect + "/15!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} else if (questionsCorrect <= 10) {
Context context = getApplicationContext();
CharSequence text = "Average results! You scored " + questionsCorrect + "/15!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} else if (questionsCorrect <= 14) {
Context context = getApplicationContext();
CharSequence text = "Good results! You scored " + questionsCorrect + "/15!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} else {
Context context = getApplicationContext();
CharSequence text = "Excellent! You scored " + questionsCorrect + "/15!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
//TextView message on bottom
endQuiz(typeName);
}
//calculate total points scored on quiz
private int calculateRightAnswers(boolean q1Name, boolean q1Location1, boolean q1Location2, boolean q2Name, boolean q2Location1, boolean q2Location2, boolean q3Name, boolean q3Location1, boolean q3Location2, boolean q4Name, boolean q4Location1, boolean q4Location2, boolean q5Name, boolean q5Location1, boolean q5Location2) {
//score before taking quiz
int pointsScored = 0;
//1 part of Q1 is correct = + 1 point
if (q1Name) {
pointsScored = pointsScored + 1;
}
//1 part of Q1 is correct = + 1 point
if (q1Location1) {
pointsScored = pointsScored + 1;
}
//1 part of Q1 is correct = + 1 point
if (q1Location2) {
pointsScored = pointsScored + 1;
}
//1 part of Q2 is correct = + 1 point
if (q2Name) {
pointsScored = pointsScored + 1;
}
//1 part of Q2 is correct = + 1 point
if (q2Location1) {
pointsScored = pointsScored + 1;
}
//1 part of Q2 is correct = + 1 point
if (q2Location2) {
pointsScored = pointsScored + 1;
}
//1 part of Q3 is correct = + 1 point
if (q3Name) {
pointsScored = pointsScored + 1;
}
//1 part of Q3 is correct = + 1 point
if (q3Location1) {
pointsScored = pointsScored + 1;
}
//1 part of Q3 is correct = + 1 point
if (q3Location2) {
pointsScored = pointsScored + 1;
}
//1 part of Q4 is correct = + 1 point
if (q4Name) {
pointsScored = pointsScored + 1;
}
//1 part of Q4 is correct = + 1 point
if (q4Location1) {
pointsScored = pointsScored + 1;
}
//1 part of Q4 is correct = + 1 point
if (q4Location2) {
pointsScored = pointsScored + 1;
}
//1 part of Q5 is correct = + 1 point
if (q5Name) {
pointsScored = pointsScored + 1;
}
//1 part of Q5 is correct = + 1 point
if (q5Location1) {
pointsScored = pointsScored + 1;
}
//1 part of Q5 is correct = + 1 point
if (q5Location2) {
pointsScored = pointsScored + 1;
}
return pointsScored;
}
private String endQuiz(String nameTyped) {
TextView endingMessageTextView = (TextView) findViewById(R.id.ending_message_text_view);
String endQuizMessage = "Thank you " + nameTyped + " for completing the quiz!";
endQuizMessage += "\nHope you enjoyed it!";
endingMessageTextView.setText(endQuizMessage);
return endQuizMessage;
}
}
谁能帮帮我?谢谢。
【问题讨论】:
-
包括你的布局 xml
-
@dltidud0108 添加了一些粗略的代码,如果您需要更多帮助,请告诉我们
-
@ditidud0108 嘿,看看这个网站少工作https://thrivethemes.com/quizbuilder/