Android:在案例中使用switch case和随机数代码

huangapple go评论119阅读模式
英文:

Android: switch case & random number code in the case

问题

以下是翻译好的内容:

Android、Java、Studio 4.0.1

我试图自己编码,但在编译器的语句中遇到了一个我不理解的问题。
基本上,当您点击单选按钮时,文本视图字段中会生成一个随机数。
我的问题是,单击单选按钮的情况下编写的随机代码不被编译器接受。

以下是代码:

package com.example.random;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

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

    public void onRadioButtonClicked(View view) {
        // 按钮是否被选中?
        boolean checked = ((RadioButton) view).isChecked();
        TextView MytextView = (TextView)findViewById(R.id.MytextView);
        int random;
        int random2;

        // 检查哪个单选按钮被点击
        switch(view.getId()) {
            case R.id.radioButton01:
                if (checked)
                    // 0或1的代码
                    random = getRandomNumber(0,1);
                    MytextView.setText(Integer.toString(random));
                    break;
            case R.id.radioButton1to6:
                if (checked)
                    // 1到6的代码
                    random2 = getRandomNumber(1,6);
                    MytextView.setText(Integer.toString(random2));
                    break;
        }
    }

    private int getRandomNumber(int min,int max) {
        return (new Random()).nextInt((max - min) + 1) + min;
    }

}
我认为这两行 'Int random;' 是错误的,但是编译器似乎希望它们在这里。
我原本认为在代码的 case 部分中 'random = getRandomNumber(0,1);' 这一行足够了,但编译器将 'random' 标记为红色。

到目前为止,编译器报告:'error: int cannot be dereferenced in the MytextView.setText(random.toString());',出现在 //0或1的代码部分。

我想理解我在这里漏掉了什么。提前感谢。

(或者更简单地问:如何在 switch/case 中编写随机函数?)
英文:

Android, java, studio 4.0.1

Trying to code by my own, I am facing an issue which I do not understand from the compiler statements.
Basically, when you click a radio button, a random number is generated in the textview field.
My problem is that the random code written in the case of the clicked radio button is not accepted by the compiler.

Here is the code:

package com.example.random;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

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

    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();
        TextView MytextView = (TextView)findViewById(R.id.MytextView);
        int random;
        int random2;

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.radioButton01:
                if (checked)
                    // 0 or 1 code
                    random = getRandomNumber(0,1);
                    MytextView.setText(random.toString());
                    break;
            case R.id.radioButton1to6:
                if (checked)
                    // 1 to 6 code
                    random2 = getRandomNumber(1,6);
                    MytextView.setText(random2.toString());
                    break;
        }
    }

    private int getRandomNumber(int min,int max) {
        return (new Random()).nextInt((max - min) + 1) + min;
    }

}

I presume that the 2 lines 'Int random;' are wrong, but the compiler seems to want to have them here.
I would have understood that the line 'random = getRandomNumber(0,1);' would have be sufficient in the case part of the code, but the compiler marks 'random' in red.

So far, the compiler says: 'error: int cannot be dereferenced in the MytextView.setText(random.toString());' of the //0 or 1 code part.

I would like to understand what I am missing here. Thanks in advance.

(Or asked simpler: how does one code a random function in a switch/case ?)

答案1

得分: 0

以下是翻译好的部分:

public void onRadioButtonClicked(View view) {
    // 按钮是否被选中?
    boolean checked = ((RadioButton) view).isChecked();
    TextView MytextView = (TextView)findViewById(R.id.MytextView);
    final Random rnd = new Random();
    // 检查哪个单选按钮被点击了
    switch(view.getId()) {
        case R.id.radioButton01:
            if (checked)
                // 0或1的代码
                MytextView.setText(String.valueOf(rnd.nextInt(3-0) + 0));
                break;
        case R.id.radioButton1to6:
            if (checked)
                // 骰子1到6的代码
                MytextView.setText("1到6");
                break;
        case R.id.radioButton1to10:
            if (checked)
                // 1到10的代码
                MytextView.setText("1到10");
                break;
    }
}

最后的 'private int getRandomNumber' 部分可以删除,不需要它。

英文:

ok I found the solution. A possible working code is:

public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();
        TextView MytextView = (TextView)findViewById(R.id.MytextView);
        final Random rnd = new Random();
        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.radioButton01:
                if (checked)
                    // 0 or 1 code
                    MytextView.setText(String.valueOf(rnd.nextInt(3-0) + 0));
                    break;
            case R.id.radioButton1to6:
                if (checked)
                    // Dice 1 to 6 code
                    MytextView.setText("1 to 6");
                    break;
            case R.id.radioButton1to10:
                if (checked)
                    // 1 to 10 code
                    MytextView.setText("1 to 10");
                    break;
        }
    }

And the 'private int getRandomNumber' at the end can be deleted. No need of it.

huangapple
  • 本文由 发表于 2020年9月10日 03:44:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63818574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定