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("");
}
}
}
相关分类