teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コード編集

2020/07/22 16:10

投稿

nine_fish397
nine_fish397

スコア8

title CHANGED
File without changes
body CHANGED
@@ -1,23 +1,39 @@
1
- タイトル通り現在String qを r = new Random().nextInt(q.length);
1
+ タイトル通り現在AndroidStudioでString qを r = new Random().nextInt(q.length);
2
2
  ((TextView) findViewById(R.id.situmon)).setText(q[r]);
3
3
  でランダムに表示させているのですがこれだと同じ問題が何回も表示されます。なのでこの質問qを乱数で表示させつつ兆宇服させない方法を教えてもらえると幸いです。お願いいたします。
4
4
  ```ここに言語を入力
5
5
  コード
6
6
  public class shindan extends AppCompatActivity {
7
+ String[] q = {
8
+ "質問1",
9
+ "質問2",
10
+ "質問3",
11
+ "質問4",
12
+ "質問5"
13
+ "質問6",
14
+ "質問7",
15
+ "質問8",
16
+ "質問9",
7
- String[] q = {"行くなら?",
17
+ "質問10",
8
- "隠れるなら?"
18
+ "質問11"
9
- };//問題
19
+ };
10
- String[] ta = {"海","ドアの裏"
20
+ String[] ta = {//回答
11
-
21
+ "1","2","3","4","5","6","7","8","9","10","11"
12
22
  };//+
13
23
 
14
- String[] ba = {"山","ベットの下"
24
+ String[] ba = {//回答
15
-
25
+ "1","2","3","4","5","6","7","8","9","10","11"
16
26
  };//-
17
27
 
18
28
  int r = 0;//乱数の保管
19
29
  int count = 10;//問題数
30
+ int score=0;//スコア
20
31
 
32
+ int n = 11;
33
+
34
+ int[] indexes = getShuffledIndexes(n);
35
+
36
+
21
37
  @Override
22
38
  protected void onCreate(Bundle savedInstanceState) {
23
39
  super.onCreate(savedInstanceState);
@@ -28,47 +44,32 @@
28
44
  displayTop();
29
45
  displayBottom();
30
46
 
31
- }
32
-
33
-
34
- public void displayTop() {//上のボタンのテキストを変えるメソッド
47
+ for (int i = 0; i < n - 1; i++) {
35
- if (r == 0) {
36
- ((TextView) findViewById(R.id.top)).setText(ta[0]);
48
+ System.out.print(indexes[i] + ", ");
37
- }else if (r==1){
38
- ((TextView) findViewById(R.id.top)).setText(ta[1]);
39
49
  }
50
+ System.out.println(indexes[n - 1]);
40
51
 
41
-
42
52
  }
43
53
 
44
- public void displayBottom() {//下のボタンのテキストを変えるメソッド
54
+ public static int[] getShuffledIndexes(int n) {
45
- if (r == 0) {
55
+ if (n <= 0) {
46
- ((TextView) findViewById(R.id.bottom)).setText(ba[0]);
47
- }else if (r==1){
56
+ return null;
48
- ((TextView) findViewById(R.id.bottom)).setText(ba[1]);
49
57
  }
50
- }
51
-
52
- public void onTop(View view) {//上のボタンのメソッド
58
+ int[] indexes = new int[n];
53
- count--;
54
- if (count > 0) {
59
+ for (int i = 0; i < n; i++) {
55
- ((TextView) findViewById(R.id.tc)).setText("のこり" + count + "問...");
56
- //出題
60
+ indexes[i] = i;
57
- r = new Random().nextInt(q.length);
58
- ((TextView) findViewById(R.id.situmon)).setText(q[r]);
59
61
  }
60
62
 
61
- }
62
- public void onBottom (View view){//下のボタンのメソッド
63
+ Random rand = new Random();
63
- count--;
64
+
64
- if (count > 0) {
65
+ for (int i = n - 1; i >= 1; i--) {
65
- ((TextView) findViewById(R.id.tc)).setText("のこり" + count + "問...");
66
- //出題
67
- r = new Random().nextInt(q.length);
66
+ int j = rand.nextInt(i + 1);
67
+ int tmp = indexes[j];
68
- ((TextView) findViewById(R.id.situmon)).setText(q[r]);
68
+ indexes[j] = indexes[i];
69
- }
69
+ indexes[i] = tmp;
70
70
  }
71
71
 
72
+ return indexes;
72
73
  }
73
74
 
74
75
  ```