简体   繁体   English

如何在Java中随机选择按钮文本?

[英]How to Randomly Choose Button Text in Java?

Friends, 朋友们

I am a Civil Engineer. 我是土木工程师。 Now i am working now in Android Developing. 现在我正在从事Android开发。 I know Java and C# but i am mentally set my Programming knowledge is only editing like Reading and Understand. 我知道Java和C#,但我的思维定式是我的编程知识仅是像Reading和Understand这样的编辑。 How to increase my skill like writing Programs Specially for C# and Java. 如何提高我的技能,例如专门为C#和Java编写程序。

Thank You. 谢谢。

then its my Question.. 然后是我的问题。

For example I have 3 buttons and 3 int values. 例如,我有3个按钮和3个int值。

How to make buttons text randomly choose from 3 int values string while button click. 单击按钮时如何使按钮文本从3个int值字符串中随机选择。

It's a Simple maths game for Android Platforms. 这是一个用于Android平台的简单数学游戏。

        TextView textA = (TextView) findViewById(R.id.textView);
        TextView textB = (TextView) findViewById(R.id.textView3);
        final Button button1 = (Button) findViewById(R.id.button1);
        final Button button2 = (Button) findViewById(R.id.button2);
        final Button button3 = (Button) findViewById(R.id.button3);


        Random rand = new Random();
        final int x = rand.nextInt(1000);
        Random randB = new Random();
        int y = randB.nextInt(1000);
        Random randw = new Random();
        int w = randw.nextInt(1000);
        Random randz = new Random();
        int z = randz.nextInt(1000);

        int A = x;
        int B = y;
        int Wrong1 = (A + w);
        int Wrong2 = (A + z);
        final int CorrectAnswer = (A + B);

        String text = "text";
        Button[] arr = {button1, button2, button3};
        Random r = new Random();
        Button b = arr[r.nextInt(arr.length)];
        b.setText(text);

        int Ans1 = CorrectAnswer;
        int Ans2 = Wrong1;
        int Ans3 = Wrong2;


        textA.setText("" + A);
        textB.setText("" + B);
        button1.setText("" + Wrong1);
        button2.setText("" + Wrong2);
        button3.setText("" + CorrectAnswer);
        final int ACAns;
        ACAns = Integer.parseInt(button3.getText().toString());
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int AAns = Integer.parseInt(button1.getText().toString());


                if ((AAns == ACAns)) {
                    Toast.makeText(getBaseContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getBaseContext(), "Your answer is Wrong!", Toast.LENGTH_SHORT).show();
                }

            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int BAns = Integer.parseInt(button2.getText().toString());
                int ACAns = Integer.parseInt(button1.getText().toString());
                if ((BAns == ACAns)) {
                    Toast.makeText(getBaseContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show();
                } else {
                    }    
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int CAns = Integer.parseInt(button3.getText().toString());
                int ACAns = Integer.parseInt(button3.getText().toString());
                if ((CAns == ACAns)) {
                    Toast.makeText(getBaseContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show();                      

                    return;
                } else {
                    Toast.makeText(getBaseContext(), "Your answer is Wrong!", Toast.LENGTH_SHORT).show();
                }

Take two arrays. 取两个数组。

String buttonNamesArray[] = {"name1", "name2", "name3"};
int indexArray[] = {0, 1, 2};

Now generate a random number by any logic and do a modulus by 3 (length of indexArray) operation to get an index in the range [0-2]. 现在,可以通过任何逻辑生成一个随机数,并通过3(indexArray的长度)进行模数运算,以获取范围为[0-2]的索引。 Now do 现在做

randomIndex = randomNumber % 3;
if(indexArray[randomIndex] != -1) {
    //assign the next button value buttonNamesArray[randomIndex]
    indexArray[randomIndex] = -1;
}

Perform this in a loop until all buttons are not over. 循环执行此操作,直到所有按钮都没有结束。

This is just the logic and not the actual code. 这只是逻辑,而不是实际的代码。

Store the string values in an array of size 3. Generate a random number between 0 and 2. Keep that value in a variable and assign the string associated to that value to your button. 将字符串值存储在大小为3的数组中。生成一个介于0和2之间的随机数。将该值保留在变量中,然后将与该值关联的字符串分配给您的按钮。 Reduce your array by deleting the string you've already assigned. 通过删除已分配的字符串来减少数组。 Then generate a random int between 0 and 1. Assign again to a button. 然后生成一个介于0和1之间的随机整数。再次分配一个按钮。 Delete the element you've just assigned and finally assign the last string value to your last button. 删除刚刚分配的元素,最后将最后一个字符串值分配给最后一个按钮。

Sorry for the lack of code but I'm on my phone, it's not really easy to code on phones. 很抱歉缺少代码,但是我在手机上,在手机上编码并不容易。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM