简单测验应用程序的评分逻辑

时间:2017-10-07 15:28:56

标签: java android

我正在制作一个简单的测验应用,但我无法获得正确的评分逻辑。 我有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;
}
}

有人可以帮帮我吗?感谢。

1 个答案:

答案 0 :(得分:0)

不确定首先失败的是什么,但这里有一两个建议 将每个问题放在自己的方法chkQuestionOne()然后在那个方法中,你知道哪两个答案是正确的,所以对于ans1的布尔值,如果正确为TRUE,对于其他正确的答案是相同的,对于错误的答案也是假的,这时也设置正确答案的int值这个correctans = 10;但是,correctANS需要全局声明它在onCreate代码行上方int correctANS = 0;保持累积运行分数correctans = correctANS + 10; 如果你想在每个问题之后立即反馈,我需要编写代码,抱歉不那么兴奋。对于测验结束时的摘要结果,如果if(ans1&amp;&amp; ans2 selected)这样的语句,那么两个答案都是正确的其他测试,如果只有一个正确,则默认如果1和2都没有选择没有正确的答案如果你喜欢这个想法并且需要更多帮助我可能会尝试在此时编写一些代码

好的,这里有一些真正粗略的代码,它不是真正完整的一个仍然忙,两个如果你有10个以上的问题我们可能会建议你使用sqlite DB存储问题和可能的答案然后从DB调用问题更多超过10个问题,你正在看一个代码管理夜间母马WORD警告我不喜欢将CheckBox设置为禁用的想法,就像我说快速和脏BOTH XML和MainActivity下面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_lightGray"
android:orientation="vertical"
tools:context="com.dwight.aquiz.MainActivity">

<RelativeLayout
    android:id="@+id/rootRL"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</RelativeLayout>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:text="Name 2 Cities in Ohio "
    android:textColor="@color/color_Black"
    android:textSize="14sp"
    android:textStyle="bold" />

<CheckBox
    android:id="@+id/chkqONEans1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:text="Canton"
    android:textColor="@color/color_Black"
    android:textSize="14sp"
    android:textStyle="bold" />

<CheckBox
    android:id="@+id/chkqTWOans2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:text="Akron"
    android:textColor="@color/color_Black"
    android:textSize="14sp"
    android:textStyle="bold" />

<CheckBox
    android:id="@+id/chkqTHREEans3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:text="Duluth"
    android:textColor="@color/color_Black"
    android:textSize="14sp"
    android:textStyle="bold" />

<Button
    android:id="@+id/btnScore"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:text="SCORE"
    android:onClick="onScore"
    android:textColor="@color/color_Black"
    android:textSize="14sp"
    android:textStyle="bold" />

<EditText
    android:id="@+id/etResult"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:focusable="false"
    android:inputType="textMultiLine"
    android:textColor="@color/color_Black"
    android:textSize="14sp"
    android:textStyle="bold" />
   </LinearLayout>

package com.aquiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

公共类MainActivity扩展了AppCompatActivity {

CheckBox chkqONEans1;
CheckBox chkqTWOans2;
CheckBox chkqTHREEans3;
Button btnScore;
String stringqONEans1,stringqTWO1ans2,stringqTHREE1ans3,master;
EditText etResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    chkqONEans1 = (CheckBox)findViewById(R.id.chkqONEans1);
    chkqTWOans2 = (CheckBox)findViewById(R.id.chkqTWOans2);
    chkqTHREEans3= (CheckBox)findViewById(R.id.chkqTHREEans3);
    btnScore = (Button)findViewById(R.id.btnScore);
    etResult = (EditText)findViewById(R.id.etResult);
}

public void onScore(View view){
    if( chkqONEans1.isChecked()){
        stringqONEans1 = "Correct Ans Canton";
        chkqONEans1.setEnabled(false);
    }
    if(chkqTWOans2.isChecked()){
        stringqTWO1ans2 = "Correct Ans Akron";
        chkqTWOans2.setEnabled(false);
    }
    if( chkqTHREEans3.isChecked()){
        stringqTHREE1ans3 = "INCORRECT Ans Duluth";
        chkqTHREEans3.setEnabled(false);
    }
    //if (chkqONEans1.isChecked() &&  chkqTWOans2.isChecked() && chkqTHREEans3.isChecked()){
        //Toast.makeText(MainActivity.this, "Only Two Answers", Toast.LENGTH_LONG).show();
        //return;
    //}

    if( chkqONEans1.isEnabled()== false && chkqTWOans2.isEnabled()==false &&  chkqTHREEans3.isEnabled()==true){
        Toast.makeText(MainActivity.this, "Go On You Have Two Answers 1 & 2", Toast.LENGTH_LONG).show();
        master = "  " + stringqONEans1 +"\n  "+stringqTWO1ans2;
        showAns();

    }else if ( chkqONEans1.isEnabled()== false && chkqTWOans2.isEnabled()==true &&  chkqTHREEans3.isEnabled()==false){
        Toast.makeText(MainActivity.this, "Go On You Have Two Answers 1 & 3", Toast.LENGTH_LONG).show();
        master = stringqONEans1 +"  "+stringqTHREE1ans3;
        showAns();

    }else if ( chkqONEans1.isEnabled()== true && chkqTWOans2.isEnabled()==false &&  chkqTHREEans3.isEnabled()==false){
        Toast.makeText(MainActivity.this, "Go On You Have Two Answers 2 & 3", Toast.LENGTH_LONG).show();
       master = stringqTWO1ans2 +"  "+stringqTHREE1ans3;
        showAns();

    }else if( chkqONEans1.isEnabled()== false &&chkqTWOans2.isEnabled()== false && chkqTHREEans3.isEnabled()== false){
        Toast.makeText(MainActivity.this, "ONLY TWO Answers ", Toast.LENGTH_LONG).show();
        chkqONEans1.setEnabled(true);
        chkqONEans1.setChecked(false);
        chkqTWOans2.setEnabled(true);
        chkqTWOans2.setChecked(false);
        chkqTHREEans3.setEnabled(true);
        chkqTHREEans3.setChecked(false);
        return;
    }
}

public void showAns(){
    etResult.setText(master);
}

}