Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数组中生成随机字符串,然后将其删除,使其不会重新生成_Java_Android_Button_Android Studio_Random - Fatal编程技术网

Java 从数组中生成随机字符串,然后将其删除,使其不会重新生成

Java 从数组中生成随机字符串,然后将其删除,使其不会重新生成,java,android,button,android-studio,random,Java,Android,Button,Android Studio,Random,使用上面的代码(使用Android Studio开发应用程序),我可以从我的数组中生成一个随机问题,如下所示 TextView textView1 = (TextView) findViewById(R.id.textView1); int randomArray = (int) (Math.random() *4); textView1.setText(questions[randomArray][0]); 下面的代码显示了我如何将答案分配给按钮,以便它们每次随机分配

使用上面的代码(使用Android Studio开发应用程序),我可以从我的数组中生成一个随机问题,如下所示

    TextView textView1 = (TextView) findViewById(R.id.textView1);
    int randomArray = (int) (Math.random() *4);
    textView1.setText(questions[randomArray][0]);
下面的代码显示了我如何将答案分配给按钮,以便它们每次随机分配到不同的按钮:

 String[][] question= new String[20][5];

 {
     question[0][0] = "what is my name?";
     question[0][1] = "Mark";
     question[0][2] = "Steven";
     question[0][3] = "Alan";
     question[0][4] = "Bob";

     question[1][0] = "what is my age?";
     question[1][1] = "30";
     question[1][2] = "31";
     question[1][3] = "32";
     question[1][4] = "33";
 }

+1表示问题不会出现在按钮中。我所需要的帮助是如何从选项中删除随机选择,这样我就不会有一个有2个、3个或4个按钮的问题,比如说steve或30等等。

您可以将字符串放入ArrayList,随机化ArrayList,然后简单地从ArrayList中删除每个按钮的顶部条目

    Button button2 = (Button)findViewById(R.id.button2);
    int randomArray1 = (int) (Math.random() *4);
    button2.setText(questions[randomArray][randomArray1+1]);

    Button button3 = (Button)findViewById(R.id.button3);
    int randomArray2 = (int) (Math.random() *4);
    button3.setText(questions[randomArray][randomArray2+1]);

    Button button4 = (Button)findViewById(R.id.button4);
    int randomArray3 = (int) (Math.random() *4);
    button4.setText(questions[randomArray][randomArray3+1]);

    Button button5 = (Button)findViewById(R.id.button5);
    int randomArray4 = (int) (Math.random() *4);
    button5.setText(questions[randomArray][randomArray4+1]);

如果您将要显示的答案放入
ArrayList
,则可以使用
Collections.shuffle()
简单地随机排列顺序。然后你会把答案一个接一个地放在他们的视图中,他们会自动地为你洗牌。通过这样做,你不必担心重复,因为你的列表中每个答案只有一份副本。所以,这只是一个问题,把他们一个接一个地放到视图中,按照他们新的随机顺序排列

看看这个……下面是它的工作原理:

mButton1.setText(mArrayList.remove(0));
mButton2.setText(mArrayList.remove(0));
ArrayList answers=new ArrayList();
答案。添加(“dave”);
答案。添加(“史蒂夫”);
答案。加上(“约翰”);
答案。加上(“卡尔”);

对于(int i=0;i)是否应将arraylist保留在与数组相同的位置?我不确定是否理解此问题。当您要填充按钮时,将生成arraylist,然后将其丢弃。您只需跟踪哪个答案是正确的。
ArrayList<String> answers = new ArrayList<>();
answers.add("dave");
answers.add("steve");
answers.add("john");
answers.add("carl");

for(int i=0; i<answers.size(); i++) {
    //answers in their original order
    Log.d("ANSWERS", answers.get(i));
}

Collections.shuffle(answers, new Random());

for(int i=0; i<answers.size(); i++) {
    //answers are now in a randomized order
    Log.d("SHUFFLED ANSWERS", answers.get(i));
}