【问题标题】:Distinct random numbers不同的随机数
【发布时间】:2019-03-07 06:04:02
【问题描述】:

所以问题如下 我设法制作了一个小程序,其中出现了随机图像,并且 3 个多项选择题作为一个广播组。 图片是从 6 个图像池中随机设置的,3 个答案来自包含所有图片名称的字符串数组。 现在第一个单选按钮总是有正确答案,因为它与设置图片的 int 具有相同的值,而其他两个是我指定的字符串值集中的随机值。

这两个问题是

一个

有时我会在两个或三个单选按钮中得到相同的答案,这是一个问题。所以我想要它,这样答案就不会重复了。

两个

第一个答案总是正确答案的事实是有问题的,所以我如何随机化它的位置并让检查按钮始终能够知道正确答案是什么。

//下面是整个java代码

package com.example.hamdanali.imageview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.LinkedHashSet;

public class imagegame extends AppCompatActivity {

ImageView pic ;
Button    btn1;
Button    btn2;
Button    btn3;
Button    check;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_imagegame);

    pic =   findViewById(R.id.Pic) ;
    btn1=   findViewById(R.id.Btn1);
    btn2=   findViewById(R.id.Btn2);
    btn3=   findViewById(R.id.Btn3);
    check=  findViewById(R.id.Check);

    final int array[]={
            R.drawable.one  ,
            R.drawable.two  ,
            R.drawable.three,
            R.drawable.four ,
            R.drawable.five ,
            R.drawable.six  , };

    final String picname[]={

            "One"  ,
            "Two"  ,
            "Three",
            "Four" ,
            "Five" ,
            "Six"  };

    check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        //to randomly select the buttons and the images

            int i=(int)(Math.floor(array.length*Math.random()));
            int j=(int)(Math.floor(array.length*Math.random()));
            int k=(int)(Math.floor(array.length*Math.random()));

            pic.setImageResource(array[i]);
            btn1.setText(picname[i]);
            btn2.setText(picname[j]);
            btn3.setText(picname[k]);



        }



        });
    }




    }

//// 下面是GUI截图

我厌倦了这个,但我不断收到这些错误

    check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            List<int> randomNums = new ArrayList<>();
            //type argument can not be of primitive type

            for (int i=0; i<3; i++) {
                int randomNum = (int)(Math.floor(array.length*Math.random()));
                while (randomNums.contains(randomNum))
                    //Cannot resolve method "contains(int)"
                    randomNum = (int)(Math.floor(array.length*Math.random()));
                randomNums.add(randomNum);
                //Cannot resolve method "add(int)"
            }

            pic.setImageResource(randomNums.get(0));
            Collections.shuffle(randomNums);
            //shuffle(java.util.List<?>)in Collections cannot be applied to    (List<int>)
            //
            btn1.setText(randomNums.get(0));
            btn2.setText(randomNums.get(1));
            btn3.setText(randomNums.get(2));
            //Cannot resolve method "get(int)"

【问题讨论】:

    标签: java android random


    【解决方案1】:

    您总是将 array[i] 设置为您的第一个答案...并且您的随机函数可以返回相同的结果两次或三次。我会这样做:

    package com.example.hamdanali.imageview;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    
    import java.util.List;
    import java.util.ArrayList;
    import java.util.LinkedHashSet;
    
    public class imagegame extends AppCompatActivity {
    
    ImageView pic ;
    Button    btn1;
    Button    btn2;
    Button    btn3;
    Button    check;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imagegame);
    
        pic =   findViewById(R.id.Pic) ;
        btn1=   findViewById(R.id.Btn1);
        btn2=   findViewById(R.id.Btn2);
        btn3=   findViewById(R.id.Btn3);
        check=  findViewById(R.id.Check);
    
        final int array[]={
                R.drawable.one  ,
                R.drawable.two  ,
                R.drawable.three,
                R.drawable.four ,
                R.drawable.five ,
                R.drawable.six  , };
    
        final String picname[]={
    
                "One"  ,
                "Two"  ,
                "Three",
                "Four" ,
                "Five" ,
                "Six"  };
    
        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
            //to randomly select the buttons and the images
            //this should fix your repeated answers
                List<Integer> randomNums = new ArrayList();
    
                for (int i=0; i<3; i++) {
                    int randomNum = (int)(Math.floor(array.length*Math.random()));
                    while (randomNums.contains(randomNum))
                        randomNum = (int)(Math.floor(array.length*Math.random()));
                    randomNums.add(randomNum);
                }
    
                pic.setImageResource(randomNums.get(0));
                Collections.shuffle(randomNums);
                btn1.setText(randomNums.get(0));
                btn2.setText(randomNums.get(1));
                btn3.setText(randomNums.get(2));
    
    
    
            }
    
    
    
        });    
    }
    }
    

    【讨论】:

    • 我试图添加评论,但我不小心用我的编辑替换了你写的内容,对不起,我还是新手 :)
    • 哦,我忘了导入arraylist。你导入了吗?并将 List 更改为 List... 我的错。现在试试我已经编辑了帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 2021-04-12
    • 2012-10-04
    相关资源
    最近更新 更多