Android中的分数计算器

时间:2015-09-18 14:39:25

标签: android calculator

我想在Android项目中实现正确和不正确的分数计算功能以及计算器的基本功能,并为不正确的分数显示输出。我有一个基本的计算器,但在功能实现方面需要帮助。

简单/基本计算器的代码是 的 MainActivity.java

public class MainActivity extends Activity {
Button buttonOne,buttonTwo,buttonThree,buttonFour,buttonFive,buttonsix,buttonsevn,buttonEight,buttonNine;
Button buttonPlus,buttonMinus,buttonDivide,buttonMultiply,buttonEqual,buttonClean;
public String str ="";
Character op = 'q';
int i,num,numtemp;
TextView showResult;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    buttonOne = (Button)findViewById(R.id.Button1);
    buttonTwo = (Button)findViewById(R.id.Button2);
    buttonThree = (Button)findViewById(R.id.Button3);
    buttonFour = (Button)findViewById(R.id.Button4);
    buttonFive = (Button)findViewById(R.id.Button5);
    buttonsix = (Button)findViewById(R.id.Button6);

    buttonEight = (Button)findViewById(R.id.Button8);
    buttonNine = (Button)findViewById(R.id.Button9);
    buttonPlus = (Button)findViewById(R.id.buttonplus);
    buttonMinus = (Button)findViewById(R.id.buttonminus);
    buttonMultiply= (Button)findViewById(R.id.buttonmulti);
    buttonDivide = (Button)findViewById(R.id.buttondivide);
    buttonEqual = (Button)findViewById(R.id.buttonequal);
    buttonClean = (Button)findViewById(R.id.buttonclear);
    showResult = (TextView)findViewById(R.id.result_id);


}
  public void button1Clicked(View v){
      insert(1);

  }

public void button2Clicked(View v){
      insert(2);

  }
  public void button3Clicked(View v){
      insert(3);

  }
  public void button4Clicked(View v){
      insert(4);

  }
  public void button5Clicked(View v){
      insert(5);

  }
  public void button6Clicked(View v){
      insert(6);
  }
  public void button7Clicked(View v){
      insert(7);

  }
  public void button8Clicked(View v){
      insert(8);

  }
  public void button9Clicked(View v){
      insert(9);

  }
  public void buttonplusClicked(View v){
      perform();
      op = '+';

  }

public void buttonminusClicked(View v){
      perform();
      op = '-';

  }
  public void buttondivideClicked(View v){
      perform();
      op = '/';

  }
  public void buttonmultiClicked(View v){
      perform();
      op = '*';

  }
  public void buttonequalClicked(View v){
      calculate();

  }

public void buttonclearClicked(View v){
      reset();
  }
  private void reset() {
    // TODO Auto-generated method stub
     str ="";
     op ='q';
     num = 0;
     numtemp = 0;
     showResult.setText("");
}
private void insert(int j) {
    // TODO Auto-generated method stub
      str = str+Integer.toString(j);
      num = Integer.valueOf(str).intValue();
      showResult.setText(str);


}
  private void perform() {
    // TODO Auto-generated method stub
    str = "";
    numtemp = num;
}
  private void calculate() {
    // TODO Auto-generated method stub
    if(op == '+')
        num = numtemp+num;
    else if(op == '-')
        num = numtemp-num;
    else if(op == '/')
        num = numtemp/num;
    else if(op == '*')
        num = numtemp*num;
    showResult.setText(""+num);
}

}

0 个答案:

没有答案