【问题标题】:Grading Logic for a Simple Quiz App简单测验应用程序的评分逻辑
【发布时间】: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/

标签: java android


【解决方案1】:

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

好的,这里有一些真正的粗略代码,它不是真正完整的,一个仍然很忙,两个如果您有超过 10 个问题,我们可能会建议您使用 sqlite DB 来存储问题和可能的答案,然后从 DB 中调用更多的问题超过 10 个问题,您正在查看代码管理噩梦警告字我不喜欢将 CheckBox 设置为禁用的想法,就像我在下面所说的快速和肮脏的 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);
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2017-01-31
    相关资源
    最近更新 更多