繁体   English   中英

更新变量

[英]Updating variables

我正在尝试制作一个小测验应用程序。 不知何故,我很难用测验的错误答案来做一个烤面包。 它不能正确更新全局变量。 我也想知道如何只用实际的错误数字来更新变量。 如果多次单击“检查我的答案”按钮,则Atm会显示例如“出现问题,请检查问题编号:3 4 3 4 3 4”。

我的activity_main看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:scaleType="centerCrop"
        android:src="@drawable/two" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.android.gtaquizapp.MainActivity">

        <TextView
            style="@style/HeaderTextViewFormat"
            android:text="@string/QuestionOne" />

        <EditText
            android:id="@+id/question_one_answer_box"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textCapWords"
            android:isScrollContainer="true"
            android:maxLength="16"
            android:maxLines="1" />

        <TextView
            style="@style/HeaderTextViewFormat"
            android:layout_marginTop="24dp"
            android:text="@string/QuestionTwo" />

        <RadioGroup
            android:id="@+id/question_two_radio_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/activity_vertical_margin">

            <RadioButton
                android:id="@+id/question_two_radio_one"
                style="@style/RadioAndCheckBoxStyle"
                android:text="@string/AnswerTwo_one" />

            <RadioButton
                android:id="@+id/question_two_radio_two"
                style="@style/RadioAndCheckBoxStyle"
                android:text="@string/AnswerTwo_two" />

            <RadioButton
                android:id="@+id/question_two_radio_three"
                style="@style/RadioAndCheckBoxStyle"
                android:text="@string/AnswerTwo_three" />

        </RadioGroup>

        <TextView
            style="@style/HeaderTextViewFormat"
            android:layout_marginTop="24dp"
            android:text="@string/QuestionThree" />

        <CheckBox
            android:id="@+id/question_three_checkbox_one"
            style="@style/RadioAndCheckBoxStyle"
            android:text="@string/AnswerThree_one" />

        <CheckBox
            android:id="@+id/question_three_checkbox_two"
            style="@style/RadioAndCheckBoxStyle"
            android:text="@string/AnswerThree_two" />

        <CheckBox
            android:id="@+id/question_three_checkbox_three"
            style="@style/RadioAndCheckBoxStyle"
            android:text="@string/AnswerThree_three" />

        <CheckBox
            android:id="@+id/question_three_checkbox_four"
            style="@style/RadioAndCheckBoxStyle"
            android:text="@string/AnswerThree_four" />

        <TextView
            style="@style/HeaderTextViewFormat"
            android:layout_marginTop="24dp"
            android:text="@string/QuestionFour" />

        <RadioGroup
            android:id="@+id/question_four_radio_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/activity_vertical_margin">

            <RadioButton
                android:id="@+id/question_four_radio_one"
                style="@style/RadioAndCheckBoxStyle"
                android:text="@string/AnswerFour_one" />

            <RadioButton
                android:id="@+id/question_four_radio_two"
                style="@style/RadioAndCheckBoxStyle"
                android:text="@string/AnswerFour_two" />

            <RadioButton
                android:id="@+id/question_four_radio_three"
                style="@style/RadioAndCheckBoxStyle"
                android:text="@string/AnswerFour_three" />

            <RadioButton
                android:id="@+id/question_four_radio_four"
                style="@style/RadioAndCheckBoxStyle"
                android:text="@string/AnswerFour_four" />
        </RadioGroup>

        <TextView
            style="@style/HeaderTextViewFormat"
            android:layout_marginTop="24dp"
            android:text="@string/QuestionFive" />

        <RadioGroup
            android:id = "@+id/question_five_radio_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/activity_vertical_margin">

            <RadioButton
                android:id="@+id/question_five_radio_two"
                style="@style/RadioAndCheckBoxStyle"
                android:text="@string/AnswerFive_two" />

            <RadioButton
                android:id="@+i`enter code here`d/question_five_radio_one"
                style="@style/RadioAndCheckBoxStyle"
                android:text="@string/AnswerFive_one" />
        </RadioGroup>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="24dp"
            android:onClick="checkAnswersMethod"
            android:text="@string/CheckAnswersButtonText" />

    </LinearLayout>
</TableLayout>

我的MainActivity看起来像这样:

package com.example.android.gtaquizapp;

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.RadioGroup;
import android.widget.Toast;

import static com.example.android.gtaquizapp.R.id.question_four_radio_four;
import static com.example.android.gtaquizapp.R.id.question_four_radio_three;
import static com.example.android.gtaquizapp.R.id.question_two_radio_three;

public class MainActivity extends AppCompatActivity {

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

/**
 *Global variables are updated by the methods answerOne();, answerTwo(); answerThree(); answerFour(); answerFive();
 */
private boolean questionOne = false;
private boolean questionTwo = false;
private boolean questionThree = false;
private boolean questionFour = false;
private boolean questionFive = false;
private String checkedTextNotAllCorrect = "Something is wrong, check question nr.:";

/**
 * This method is executed if the button "Check My Answers" is clicked.
 * It creates a toast with the String checkedTextAllCorrect if all answers are correct or with checkedTextNotAllCorrect if something is wrong
 */
public void checkAnswersMethod(View v) {
    String checkedTextAllCorrect = "Everything is correct, great job!";
    answerOne();
    answerTwo();
    answerThree();
    answerFour();
    answerFive();

    if (questionOne & questionTwo & questionThree & questionFour & questionFive) {
        Toast.makeText(getApplicationContext(), checkedTextAllCorrect, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getApplicationContext(), checkedTextNotAllCorrect, Toast.LENGTH_SHORT).show();
    }
}

/**
 * Updates the global variable questionOne if the answer was correct
 * Updates the global variable checkedTextNotAllCorrect with the questions number if the answer was not correct
 */
public void answerOne() {
    EditText questionOneAnswer = (EditText) findViewById(R.id.question_one_answer_box);
    if (questionOneAnswer.getText().toString().equals("Grand Theft Auto")) {
        questionOne = true;
    } else {
        checkedTextNotAllCorrect += " 1";
    }
}

/**
 * Updates the global variable questionTwo if the answer was correct
 * Updates the global variable checkedTextNotAllCorrect with the questions number if the answer was not correct
 */
public void answerTwo() {
    RadioGroup questionTwoRadioGroup = (RadioGroup) findViewById(R.id.question_two_radio_group);
    int radioButtonID = questionTwoRadioGroup.getCheckedRadioButtonId();

    switch (radioButtonID) {
        case R.id.question_two_radio_one | question_two_radio_three:
            checkedTextNotAllCorrect += " 2";

        case R.id.question_two_radio_two:
            questionTwo = true;
    }
}

/**
 * Updates the global variable questionThree if the answer was correct
 * Updates the global variable checkedTextNotAllCorrect with the questions number if the answer was not correct
 */
public void answerThree() {
    CheckBox checkBoxOne = (CheckBox) findViewById(R.id.question_three_checkbox_one);
    CheckBox checkBoxTwo = (CheckBox) findViewById(R.id.question_three_checkbox_two);
    CheckBox checkBoxThree = (CheckBox) findViewById(R.id.question_three_checkbox_three);
    CheckBox checkBoxFour = (CheckBox) findViewById(R.id.question_three_checkbox_four);

    if (checkBoxTwo.isChecked() & checkBoxThree.isChecked() & checkBoxFour.isChecked() & !checkBoxOne.isChecked()) {
        questionThree = true;
    } else {
        checkedTextNotAllCorrect += " 3";
    }

}

/**
 * Updates the global variable questionFour if the answer was correct
 * Updates the global variable checkedTextNotAllCorrect with the questions number if the answer was not correct
 */
public void answerFour() {
    RadioGroup questionFourRadioGroup = (RadioGroup) findViewById(R.id.question_four_radio_group);
    int radioButtonID = questionFourRadioGroup.getCheckedRadioButtonId();

    switch (radioButtonID) {
        case R.id.question_four_radio_one:
            questionFour = true;

        case R.id.question_four_radio_two | question_four_radio_three | question_four_radio_four:
            checkedTextNotAllCorrect += " 4";
    }

}

/**
 * Updates the global variable questionFive if the answer was correct
 * Updates the global variable checkedTextNotAllCorrect with the questions number if the answer was not correct
 */
public void answerFive() {
    RadioGroup questionFiveRadioGroup = (RadioGroup) findViewById(R.id.question_five_radio_group);
    int radioButtonID = questionFiveRadioGroup.getCheckedRadioButtonId();

    switch (radioButtonID) {
        case R.id.question_five_radio_one:
            checkedTextNotAllCorrect += " 5";

        case R.id.question_five_radio_two:
            questionFive = true;

    }

}

}

对于方法answerTwo()answerFour()answerFive()您使用的是switch-case而没有break语句,这会引起问题。

在每种case添加break语句以进行切换。 参见下面的示例answerTwo()

public void answerTwo() {
RadioGroup questionTwoRadioGroup = (RadioGroup) findViewById(R.id.question_two_radio_group);
int radioButtonID = questionTwoRadioGroup.getCheckedRadioButtonId();

switch (radioButtonID) {
    case R.id.question_two_radio_one | question_two_radio_three:
        checkedTextNotAllCorrect += " 2";
        break;
    case R.id.question_two_radio_two:
        questionTwo = true;
        break;
}

}

对使用switch-case其他方法执行相同的操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM