字符串变量和同名的双变量是不同的?

时间:2012-02-14 05:08:15

标签: android

当我引用display1变量时,我怎么能这样做,我指的是double,而不是字符串?程序仍然将display1称为字符串。我想要双倍。

[code]
package rechee.cool;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class HelloAndroidActivity extends Activity {
    /** Called when the activity is first created. */

    double counter1=0;
    double counter2=0;



     public EditText display;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Associate the button variable with the xml reference



        display= (EditText) findViewById(R.id.editText1);}
             //

             ////

//////////////////////////////

            public void onClick(View v) {
                switch(v.getId()){
                    case R.id.bOne:
                    display.append("1");
                        break;
                    case R.id.bTwo:

                        display.append("2");
                        break;
                    case R.id.bThree:
                        display.append("3");
                        break;
                    case R.id.bFour:
                        display.append("4");
                        break;
                    case R.id.bFive:
                        display.append("5");
                        break;
                    case R.id.bSix:
                        display.append("6");
                        break;

                    case R.id.bSeven:
                        display.append("7");
                        break;
                    case R.id.bEight:
                        display.append("8");
                        break;
                    case R.id.bNine:
                        display.append("9");
                        break;
                    case R.id.bZero:
                        display.append("0");
                        break;
                    case R.id.bPoint:
                        display.append(".");
                        break;
                    case R.id.bClear:
                        display.setText("");
                        break;
                    case R.id.bAdd:
                        // to get string of EditText 
                        String display1= display.getText().toString();
                        Double.parseDouble(display1);
                        //to test if display1 is double
                        counter1+= display1;
[/code]

非常感谢帮助。

2 个答案:

答案 0 :(得分:1)

        String display1= display.getText().toString();
        try {
            double dDisplay1 = Double.parseDouble(display1);
            //to test if display1 is double
            counter1+= dDisplay1;

        } catch (NumberFormatException e) {

            //if couldn't parse to double it will throw
            //this (NumberFormatException) exception and you can catch it
            //print something like "text is not double" or do whatever you want.
        }

答案 1 :(得分:0)

您需要将Double.parseDouble()的结果分配给变量,或者以某种方式使用该表达式的结果,现在方法调用对您的代码没有影响。

变化:

String display1= display.getText().toString();
Double.parseDouble(display1);
//to test if display1 is double
counter1+= display1;

要:

String display1= display.getText().toString();
double displayValue = Double.parseDouble(display1);
counter1+= displayValue;