【问题标题】:Can't view the full equation for the android calculator无法查看 android 计算器的完整方程式
【发布时间】:2014-06-18 07:32:20
【问题描述】:

我已经完成了使用 eclipse ADT 的 android 计算器 它工作正常,但 我在计算时无法查看整个方程,这意味着在测试期间我计算时我无法查看整个方程 x+y=z em> 我只能查看 X,然后当我按 + 时,我在屏幕上看不到,然后我在单击它时看到 Y,然后我得到答案 Z 为什么我不能查看整个等式? X+Y=Z这是我的java代码

package com.zee.sampleCalculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.zee.sampleCalculator.R;


public class MainActivity extends Activity {

 public String str ="";
 Character op = 'q';
 int i,num,number;
 EditText showResult;

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

        showResult = (EditText)findViewById(R.id.result_id);  
    }

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

    public void btn2Clicked(View v){
       insert(2);   
    }

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

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

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

    public void btn6Clicked(View v){
       insert(6);
    }

    public void btn7Clicked(View v){
       insert(7);
    }

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

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

    public void btn0Clicked(View v){
       insert(0);   
    }

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

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

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

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

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

    public void btnclearClicked(View v){
       reset();
    }

    private void reset() {
       // TODO Auto-generated method stub
       str ="";
       op ='q';
       num = 0;
       number = 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 = "";
      number = num;
   }

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

}

提前致谢

【问题讨论】:

    标签: java android eclipse calculator eclipse-adt


    【解决方案1】:

    将您的 perform() 方法更改为:

    private void perform(String op){
       str = str + op;
       number = num;
       showResult.setText(str);
    }
    

    并使用

    perform(op);
    

    在你的btnplusClicked(View v)、btnminusClicked(View v)、btndivideClicked(View v)、btnmultiClicked(View v)中调用它。

    现在 perform() 方法将添加到 str +-/* 并且不要将其重置为 " "。

    在你的计算方法中这样做:

    private void calculate(){
       int result;
    
       if(op == '+')
          result = number+num;
       else if(op == '-')
          result = number-num;
       else if(op == '/')
          result = number/num;
       else if(op == '*')
          result = number*num;
       showResult.setText(number + " " + op + " " + num + " = " + result);
    }
    

    如果您有不明白的地方,请不要着急问。我希望这对你有帮助:)

    EDIT1:

    我犯了一个小错误。您使用的是 char,而不是 String。

    public void btnplusClicked(View v){
       op = '+';
       perform(op);
    }
    
    private void perform(char op){
       ...
    }
    

    在计算()中

    private void calculate(){
       int result = 0;
       ...
    }
    

    现在,它应该可以工作了。但据我所知,您不了解 Java 及其类型的主要基础知识。 Android 是基于 Java 的,在您开始使用 Android 编程之前,有必要从 Java 中了解一点。

    【讨论】:

    • 非常感谢一开始我按照你在第一个代码中所说的那样做了,但在第二个 preform(op);你是不是想让它像这样public void btnplusClicked(View v){ perform(op); op = '+'; } 因为当我这样做时出现了一个错误,即 MainActivity 类型中的方法 perform(String) 不适用于 arguments(Character) 和最后的代码我在 ShowResult.setText 中遇到错误最后的结果被标记为错误 局部变量结果可能尚未初始化 希望您能帮助我解决这些错误. 再次感谢。
    • 非常感谢我现在会再次检查:D 我知道 Java,但我仍然需要学习更多。
    【解决方案2】:

    您的代码完全按照您的要求执行。为了查看整个方程式,您必须更改一些内容。

    private void perform() {
        // TODO Auto-generated method stub
        str = ""; //get the value in showResult by doing showResult.getText()
                  // and keep appending to the string.
        number = num;
       }
    

    也在你的:

    private void calculate() {
        // TODO Auto-generated method stub
        if(op == '+')
         num = number+num;
        else if(op == '-')
         num = number-num;
        else if(op == '/')
         num = number/num;
        else if(op == '*')
         num = number*num;
        showResult.setText(number+ " " + op + " " + num + " " + "=" + num); // change this to 
       }
    

    我还没有尝试过,但这应该会让你朝着正确的方向前进。

    【讨论】:

    • 感谢@android_Muncher 的帮助.. 但如果你能告诉我更多关于第一个代码的信息,我没有得到它。在 show.Result.setText 的第二个代码中,我在这个区域遇到语法错误 + num " " + 我应该删除 " " 吗?抱歉,我对这件事很陌生,我不是 Java 方面的专家。
    猜你喜欢
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多