下記のコードで問題を15問作りました。
そのうち15問中10問だけクイズを出したいときはどのように書き換えれば宜しいでしょうか?
また、出題の順番もランダムで出したいと思っております。
大変分かりにくくて申し訳ありませんが回答いただけますと助かりますので宜しくお願い致します。
・java
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class MyQuiz extends ActionBarActivity {
public final static String EXTRA_MYSCORE = "com.example.taguchi.myquizapp.MYSCORE"; private ArrayList<String[]> quizSet = new ArrayList<String[]>(); private TextView scoreText; private TextView qText; private Button a0Button; private Button a1Button; private Button a2Button; private Button nextButton; private int currentQuiz = 0; private int score = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_quiz); loadQuizSet(); getViews(); setQuiz(); } private void showScore() { scoreText.setText("Score: " + score + " / " + quizSet.size()); } private void setQuiz() { qText.setText(quizSet.get(currentQuiz)[0]); ArrayList<String> answers = new ArrayList<String>(); for (int i = 1; i <= 3; i++) { answers.add(quizSet.get(currentQuiz)[i]); } Collections.shuffle(answers); a0Button.setText(answers.get(0)); a1Button.setText(answers.get(1)); a2Button.setText(answers.get(2)); a0Button.setEnabled(true); a1Button.setEnabled(true); a2Button.setEnabled(true); nextButton.setEnabled(false); showScore(); } public void checkAnswer(View view) { // answer? Button clickedButton = (Button) view; String clickedAnswer = clickedButton.getText().toString(); // judge if (clickedAnswer.equals(quizSet.get(currentQuiz)[1])) { clickedButton.setText("◯ " + clickedAnswer); score++; } else { clickedButton.setText("× " + clickedAnswer); } showScore(); // button a0Button.setEnabled(false); a1Button.setEnabled(false); a2Button.setEnabled(false); nextButton.setEnabled(true); // next quiz currentQuiz++; if (currentQuiz == quizSet.size()) { nextButton.setText("Check result"); } } public void goNext(View view) { if (currentQuiz == quizSet.size()) { // show result Intent intent = new Intent(this, MyResult.class); intent.putExtra(EXTRA_MYSCORE, score + " / " + quizSet.size()); startActivity(intent); } else { setQuiz(); } } @Override public void onResume() { super.onResume(); nextButton.setText("Next"); currentQuiz = 0; score = 0; setQuiz(); } private void getViews() { scoreText = (TextView) findViewById(R.id.scoreText); qText = (TextView) findViewById(R.id.qText); a0Button = (Button) findViewById(R.id.a0Button); a1Button = (Button) findViewById(R.id.a1Button); a2Button = (Button) findViewById(R.id.a2Button); nextButton = (Button) findViewById(R.id.nextButton); } private void loadQuizSet() { InputStream inputStream = null; BufferedReader bufferedReader = null; try { inputStream = getAssets().open("quiz.txt"); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String s; while ((s = bufferedReader.readLine()) != null) { quizSet.add(s.split("\t")); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (inputStream != null) inputStream.close(); if (bufferedReader != null) bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
}
・txt
what is A? A0 A1 A2
what is B? B0 B1 B2
what is C? C0 C1 C2
what is D? D0 D1 D2
what is E? E0 E1 E2
what is F? F0 F1 F2
what is G? G0 G1 G2
what is H? H0 H1 H2
what is I? I0 I1 I2
what is J? J0 J1 J2
what is K? K0 K1 K2
what is L? L0 L1 L2
what is M? M0 M1 M2
what is N? N0 N1 N2
what is O? O0 O1 O2
・xml
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainQuiz">
</LinearLayout><TextView android:id="@+id/scoreText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" /> <TextView android:id="@+id/qText" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:gravity="center" android:textSize="32sp" android:textStyle="bold" /> <Button android:id="@+id/a0Button" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:onClick="checkAnswer" /> <Button android:id="@+id/a1Button" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:onClick="checkAnswer" /> <Button android:id="@+id/a2Button" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:onClick="checkAnswer" /> <Button android:id="@+id/a3Button" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:onClick="checkAnswer" /> <Button android:id="@+id/nextButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="goNext" android:layout_gravity="right" android:layout_marginTop="8dp" android:text="@string/next_button" />
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/10/11 14:52
2015/10/11 15:17
2015/10/11 15:34
2015/10/11 15:43