简化Java和Android Studio中的一些代码

时间:2017-07-10 12:14:29

标签: java android

我正在尝试创建类似的东西:单击按钮后,程序应显示四个随机数(1到9之间),但有一些特殊性。在一组条件(操作)之后,最终结果是22.我已经编写了代码,但我认为它对于使用而言太大了,而且我想要改变一些小事情。例如,不是多次单击按钮,直到出现正确的数字组,我希望它在第一次单击时自动显示这四个“魔术”数字。我不知道该怎么做:

public class MainActivity extends AppCompatActivity {  

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


    }    public void button_action (View v){ int TF = 22;

    Random a = new Random();
    int i1 = a.nextInt(10 - 1) + 1;
    TextView tv = (TextView) findViewById(R.id.textView1);

    Random b = new Random();
    int i2 = b.nextInt(10-1)+1;
    TextView tv2 = (TextView) findViewById(R.id.textView2);

    Random c = new Random();
    int i3 = c.nextInt(10-1)+1;
    TextView tv3 = (TextView) findViewById(R.id.textView3);

    Random d = new Random();
    int i4 = d.nextInt(10-1)+1;
    TextView tv4 = (TextView) findViewById(R.id.textView4);

    int soma = i1 + i2 + i3 + i4;
    int mult0 = i1 * i2 * i3 * i4;
    int mult1 = (i1*i2*i3)+i4;
    int mult2 = (i1*i2)+i3+i4;
    int mult3 = i1 * (i2 + i3 + i4);
    int mult4 = i2 * (i1+i3+i4);
    int mult5 = i3 * (i1+i2+i4);
    int mult6 = i4 * (i1+i2+i3);
    int mult7 = (i1*i3)+i2+i4;
    int mult8 = (i1*i4)+i2+i3;
    int mult9 = (i2*i3)+i1+i4;
    int mult10 = (i2*i4)+i1+i3;
    int mult11 = (i4*i3)+i1+i2;
    int mult12 = (i1*i3*i4)+i2;
    int mult13 = (i1*i2*i4)+i3;
    int mult14 = (i4*i2*i3)+i1;

    if (soma == TF){
        tv.setText(String.valueOf(i1));
        tv2.setText(String.valueOf(i2));
        tv3.setText(String.valueOf(i3));
        tv4.setText(String.valueOf(i4));}

    if (mult0 == TF){
        tv.setText(String.valueOf(i1));
        tv2.setText(String.valueOf(i2));
        tv3.setText(String.valueOf(i3));
        tv4.setText(String.valueOf(i4));}

    if (mult1 == TF){
        tv.setText(String.valueOf(i1));
        tv2.setText(String.valueOf(i2));
        tv3.setText(String.valueOf(i3));
        tv4.setText(String.valueOf(i4));}

    if (mult2 == TF){
        tv.setText(String.valueOf(i1));
        tv2.setText(String.valueOf(i2));
        tv3.setText(String.valueOf(i3));
        tv4.setText(String.valueOf(i4));}

    if (mult3 == TF){
        tv.setText(String.valueOf(i1));
        tv2.setText(String.valueOf(i2));
        tv3.setText(String.valueOf(i3));
        tv4.setText(String.valueOf(i4));}

    if (mult4 == TF){
        tv.setText(String.valueOf(i1));
        tv2.setText(String.valueOf(i2));
        tv3.setText(String.valueOf(i3));
        tv4.setText(String.valueOf(i4));}   ... } 
 }

有什么建议吗?

0 个答案:

没有答案