更新变量

时间:2016-10-29 10:58:43

标签: java android variables

我正在尝试制作一个小测验应用。不知怎的,我用一个错误的测验答案创建一个祝酒词有点麻烦。它没有正确更新全局变量。另外,我想知道如何仅使用实际错误的数字来更新变量。 Atm它显示例如"出了点问题,检查问题nr。: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;

    }

}

}

1 个答案:

答案 0 :(得分:0)

对于方法answerTwo()answerFour()answerFive(),您使用的switch-case没有导致问题的break语句。

在每个break之后为切换添加case语句。请参阅下面的示例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的其他方法执行相同操作。