• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

24点小游戏,等于号没反应

public class littlegame extends AppCompatActivity {
    TextView txt1, txt2, txt3, txt4, num1, num2, num3, num4, answer1;
    boolean clear_flag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_littlegame);
        txt1 = (TextView) findViewById(R.id.textView);
        txt2 = (TextView) findViewById(R.id.textView2);
        txt3 = (TextView) findViewById(R.id.textView3);
        txt4 = (TextView) findViewById(R.id.textView4);
        answer1=(TextView)findViewById(R.id.textView1) ;

        num1 = (TextView) findViewById(R.id.button1);
        num2 = (TextView) findViewById(R.id.button2);
        num3 = (TextView) findViewById(R.id.button3);
        num4 = (TextView) findViewById(R.id.button4);
    }
    public void Onbtnnew(View n) {
        int result1 = (int) (Math.random() * 10);
        txt1.setText(Integer.toString(result1));
        int result2 = (int) (Math.random() * 10);
        txt2.setText(Integer.toString(result2));
        int result3 = (int) (Math.random() * 10);
        txt3.setText(Integer.toString(result3));
        int result4 = (int) (Math.random() * 10);
        txt4.setText(Integer.toString(result4));

        answer1.setText("");

        num1.setText(Integer.toString(result1));
        num2.setText(Integer.toString(result2));
        num3.setText(Integer.toString(result3));
        num4.setText(Integer.toString(result4));

        findViewById(R.id.button1).setVisibility(View.VISIBLE);
        findViewById(R.id.button2).setVisibility(View.VISIBLE);
        findViewById(R.id.button3).setVisibility(View.VISIBLE);
        findViewById(R.id.button4).setVisibility(View.VISIBLE);

    }
    public void Onbtnback(View b) {
        findViewById(R.id.button1).setVisibility(View.VISIBLE);
        findViewById(R.id.button2).setVisibility(View.VISIBLE);
        findViewById(R.id.button3).setVisibility(View.VISIBLE);
        findViewById(R.id.button4).setVisibility(View.VISIBLE);

        answer1.setText("");

    }
    public void onClick(View c) {
        String answer = answer1.getText().toString();
        switch (c.getId()) {
            case R.id.button1:
            case R.id.button2:
            case R.id.button3:
            case R.id.button4:
                if (clear_flag) {
                    clear_flag = false;
                    answer = "";
                    answer1.setText("");
                }
                answer1.setText(answer + ((Button) c).getText() + " ");
                break;
            case R.id.button5:
            case R.id.button6:
            case R.id.button7:
            case R.id.button8:
                if (clear_flag) {
                    clear_flag = false;
                    answer1.setText("");
                }
                answer1.setText(answer + ((Button)c).getText() + " ");
                break;
        }
        switch (c.getId()) {
            case R.id.button1:
                findViewById(R.id.button1).setVisibility(View.INVISIBLE);
                break;
            case R.id.button2:
                findViewById(R.id.button2).setVisibility(View.INVISIBLE);
                break;
            case R.id.button3:
                findViewById(R.id.button3).setVisibility(View.INVISIBLE);
                break;
            case R.id.button4:
                findViewById(R.id.button4).setVisibility(View.INVISIBLE);
                break;
        }
    }

    public void onequal(View v){
        String exp = answer1.getText().toString();
        if(exp == null || exp.equals("")){
            return;
        }
        if(exp.contains(" ")){
            return;
        }
        if(clear_flag){
            clear_flag = false;
            return;
        }
        clear_flag = true;
        String str_1 = exp.substring(0,exp.indexOf(" ")); // 运算符前面的字符
        String str_op = exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2); //获取到运算符
        String str_2 = exp.substring(exp.indexOf(" ")+ 3);   //运算符后面的数字
        double result = 0;
        if(!str_1.equals("")&&!str_2.equals("")){
            double num_1 = Double.parseDouble(str_1);   //先将str_1、str_1强制转化为double类型
            double num_2 = Double.parseDouble(str_2);

            if (str_op.equals("+")){
                result = num_1 + num_2;
            }else if (str_op.equals("-")){
                result = num_1 - num_2;
            }else if (str_op.equals("*")){
                result = num_1 * num_2;
            }else if (str_op.equals("/")){
                if(num_2 == 0){
                    result = 0;
                }else {
                    result = num_1 / num_2;
                }
            }
            if(!str_1.contains(".")&&!str_2.contains(".")&&!str_op.equals("/")){
                int r = (int) result;
                answer1.setText(r+"");
            }else{
                answer1.setText(result+"");
            }
        }else if(!str_1.equals("")&&str_2.equals("")){
            answer1.setText(exp);
        }else if(str_1.equals("")&&!str_2.equals("")) {
            double num_2 = Double.parseDouble(str_2);
            if (str_op.equals("+")){
                result = 0 + num_2;
            }else if (str_op.equals("-")){
                result = 0 - num_2;
            }else if (str_op.equals("*")){
                result = 0;
            }else if (str_op.equals("/")){
                result = 0;
            }
            if(!str_2.contains(".")){
                int r = (int) result;
                answer1.setText(r+" ");
            }else{
                 answer1.setText(result+" ");
            }
        }else{
            answer1.setText("");
        }

    }

}

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!