乘以零,双无数

时间:2012-01-14 22:13:35

标签: android double

我正在尝试为android构建一个计算器,我试图将两个数字相乘。 如果我给出1号并让2号打开,我想得到零作为答案。

我首先想到的可能是没有双重值的问题。 所以我试过:if(Double.isNaN(number1)){number1 = 0; }    但它没有用。

我如何实现这一目标?

R.layout.main:

         <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!-- 1 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="number:" />
    <EditText
        android:id="@+id/number1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="0"
        android:inputType="number"
        android:layout_marginLeft="20dp" />
</LinearLayout>

<!-- 2 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="number:" />
    <EditText
        android:id="@+id/number2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="0"
        android:inputType="number"
        android:layout_marginLeft="20dp" />
</LinearLayout>


<!-- multiply -->   
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"  >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="multiply:" />
    <TextView
        android:id="@+id/multiplydisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_marginLeft="20dp" />
</LinearLayout>


<Button
    android:id="@+id/btncalculate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Calculate"/>

<Button
    android:id="@+id/btnreset"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Reset" />

     </LinearLayout>

TipcalcActivity.java

package com.tip.calc;

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


    public class TipcalcActivity extends Activity   {
private EditText number1;
private EditText number2;
private TextView multiplydisplay;
private Button btncalculate;
private Button btnreset;
private double number1calc = 0;
private double number2calc = 0;
private double multiply     = 0;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initControls();
}

private void initControls() {
    number1 = (EditText)findViewById(R.id.number1);
    number2 = (EditText)findViewById(R.id.number2);
    multiplydisplay = (TextView)findViewById(R.id.multiplydisplay);
    btncalculate = (Button)findViewById(R.id.btncalculate);
    btnreset = (Button)findViewById(R.id.btnreset);
    btncalculate.setOnClickListener(new Button.OnClickListener() { public void 
     onClick (View v){ calculate(); }});
    btnreset.setOnClickListener(new Button.OnClickListener() { public void   
  onClick (View v){ reset(); }});
}

private void calculate()    {
    number1calc=Double.parseDouble(number1.getText().toString());
    number2calc=Double.parseDouble(number2.getText().toString());
    multiply=(number1calc*number2calc);
    multiplydisplay.setText(Double.toString(multiply));
}

private void reset()    {
    multiplydisplay.setText("");
    number1.setText("");
    number2.setText("");
}
}

2 个答案:

答案 0 :(得分:2)

您应该检查值的文本字段的内容是否与“”不同。如果不是,只需指定值0.

计算:

private void calculate()    {
    number1calc=(number1.getText().toString() != "" ) ? Double.parseDouble(number1.getText().toString()) : 0;
    number2calc=(number2.getText().toString() != "" ) ? Double.parseDouble(number2.getText().toString()) : 0;
    multiply=(number1calc*number2calc);
    multiplydisplay.setText(Double.toString(multiply));

}

答案 1 :(得分:1)

if(numberOne.getText().toString().trim().length() < 1 || numberTwo.getText().toString().length() < 1){
    multiplyDisplay.setText("0");
}
else{
    //default handling
}

如果edittext留空,这显然不是数字(NaN)。在Java中使用NaN执行任何操作都会导致此操作的结果为NaN。