Skip to content

Instantly share code, notes, and snippets.

@codingwithsara
Created October 16, 2018 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingwithsara/2270f0232b4434d7adaa045925f48e11 to your computer and use it in GitHub Desktop.
Save codingwithsara/2270f0232b4434d7adaa045925f48e11 to your computer and use it in GitHub Desktop.
第4回
package com.codingwithsara.quizapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private TextView countLabel;
private TextView questionLabel;
private Button answerBtn1;
private Button answerBtn2;
private Button answerBtn3;
private Button answerBtn4;
private String rightAnswer;
private int rightAnswerCount = 0;
private int quizCount = 1;
ArrayList<ArrayList<String>> quizArray = new ArrayList<>();
String quizData[][] = {
// {"都道府県名", "正解", "選択肢1", "選択肢2", "選択肢3"}
{"北海道", "札幌市", "長崎市", "福島市", "前橋市"},
{"青森県", "青森市", "広島市", "甲府市", "岡山市"},
{"岩手県", "盛岡市","大分市", "秋田市", "福岡市"},
{"宮城県", "仙台市", "水戸市", "岐阜市", "福井市"},
{"秋田県", "秋田市","横浜市", "鳥取市", "仙台市"},
{"山形県", "山形市","青森市", "山口市", "奈良市"},
{"福島県", "福島市", "盛岡市", "新宿区", "京都市"},
{"茨城県", "水戸市", "金沢市", "名古屋市", "奈良市"},
{"栃木県", "宇都宮市", "札幌市", "岡山市", "奈良市"},
{"群馬県", "前橋市", "福岡市", "松江市", "福井市"},
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countLabel = findViewById(R.id.countLabel);
questionLabel = findViewById(R.id.questionLabel);
answerBtn1 = findViewById(R.id.answerBtn1);
answerBtn2 = findViewById(R.id.answerBtn2);
answerBtn3 = findViewById(R.id.answerBtn3);
answerBtn4 = findViewById(R.id.answerBtn4);
// クイズデータquizDataからクイズ出題用のquizArrayを作成する
for (int i = 0; i < quizData.length; i++) {
// 新しいArrayListを準備
ArrayList<String> tmpArray = new ArrayList<>();
// クイズデータを追加
tmpArray.add(quizData[i][0]); // 都道府県名
tmpArray.add(quizData[i][1]); // 正解
tmpArray.add(quizData[i][2]); // 選択肢1
tmpArray.add(quizData[i][3]); // 選択肢2
tmpArray.add(quizData[i][4]); // 選択肢3
// tmpArrayをquizArrayに追加する
quizArray.add(tmpArray);
}
showNextQuiz();
}
public void showNextQuiz() {
// クイズカウントラベルを更新
countLabel.setText("Q" + quizCount);
// ランダムな数字を取得
Random random = new Random();
int randomNum = random.nextInt(quizArray.size());
// randomNumを使って、quizArrayからクイズを一つ取り出す
ArrayList<String> quiz = quizArray.get(randomNum);
// 問題文(都道府県名)を表示
questionLabel.setText(quiz.get(0));
// 正解をrightAnswerにセット
rightAnswer = quiz.get(1);
// クイズ配列から問題文(都道府県名)を削除
quiz.remove(0);
// 正解と選択肢3つをシャッフル
Collections.shuffle(quiz);
// 回答ボタンに正解と選択肢3つを表示
answerBtn1.setText(quiz.get(0));
answerBtn2.setText(quiz.get(1));
answerBtn3.setText(quiz.get(2));
answerBtn4.setText(quiz.get(3));
// このクイズをquizArrayから削除
quizArray.remove(randomNum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment