无法弄清楚如何摆脱NaN

时间:2010-12-28 06:16:36

标签: java android

好的,所以我正在构建计算3个输入框的应用程序。我在用0值进行一些计算时得到一个NaN 这是main.xml

<?xml version="1.0" encoding="utf-8"?>


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/ScrollView01" >




<AbsoluteLayout android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">



    <EditText android:id="@+id/InputNumber01"
        android:numeric="integer|decimal" android:imeOptions="actionNext"
        android:textStyle="bold" android:hint="Service Call"
        android:layout_x="5dip" android:layout_y="5px" android:layout_height="55dip"
        android:textSize="20dip" android:layout_width="195dip"
        android:selectAllOnFocus="true" android:inputType="numberDecimal"/>



    <EditText android:id="@+id/InputNumber02"
        android:numeric="integer|decimal" android:inputType="numberDecimal"
        android:textStyle="bold" android:hint="Parts" android:layout_x="5dip"
        android:layout_y="90px" android:layout_height="55dip"
        android:textSize="20dip" android:layout_width="195dip"
        android:selectAllOnFocus="true" android:imeOptions="actionNext"/>



    <EditText android:id="@+id/InputNumber03" android:numeric="integer|decimal"
        android:inputType="numberDecimal" android:textStyle="bold"
        android:hint="Labor" android:layout_x="5dip" android:layout_y="175px"
        android:layout_height="55dip" android:textSize="20dip"
        android:layout_width="195dip" android:selectAllOnFocus="true"
        android:imeOptions="actionDone"/>

    <Button android:text="Calculate" android:id="@+id/actionButton"

        android:textStyle="bold" android:layout_height="50dip"
        android:layout_width="fill_parent" android:layout_y="300px"
        android:textSize="15dip" />


    <TextView android:id="@+id/TextResult" android:layout_height="wrap_content"
        android:numeric="integer|decimal" android:textStyle="bold"
        android:layout_width="85dip" android:layout_x="225dip"
        android:layout_y="175dip" android:gravity="left" android:textSize="18dip"
        android:textColor="#1d9ee2" />



    <TextView android:layout_height="wrap_content" android:id="@+id/TextView04"
        android:text="Total:" android:textStyle="bold" android:gravity="right"
        android:layout_width="55dip" android:layout_y="175dip"
        android:layout_x="165dip" android:textSize="18dip" />

    <TextView android:layout_height="wrap_content" android:id="@+id/TextView05"
        android:text="Total Labor:" android:layout_y="260dip"
        android:layout_width="95dip" android:layout_x="5dip" android:gravity="right" />

    <TextView android:text="@+id/TotalLabor"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:id="@+id/TotalLabor" android:layout_y="260dip"
        android:layout_x="110dip" />

    <TextView android:layout_height="wrap_content" android:id="@+id/TextView06"
        android:layout_x="5dip" android:text="Total Services:"
        android:layout_y="280dip" android:layout_width="95dip"
        android:gravity="right" />

    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:id="@+id/TotalServices"
        android:text="@+id/TotalServices" android:layout_y="280dip"
        android:layout_x="110dip" />

    <TextView android:id="@+id/TextView07" android:text="SC Tax %:"
        android:layout_y="300dip" android:layout_height="wrap_content"
        android:layout_x="5dip" android:layout_width="95dip" android:gravity="right" />

    <TextView android:text="@+id/TextView08"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:id="@+id/SCTaxPercent" android:layout_y="300dip"
        android:layout_x="110dip" />

    <TextView android:layout_height="wrap_content" android:id="@+id/TextView08"
        android:text="Sales Tax:" android:textStyle="bold" android:layout_y="175dip"
        android:layout_x="5dip" android:textSize="18dip"
        android:layout_width="wrap_content" android:gravity="right" />

    <TextView android:layout_height="wrap_content" android:id="@+id/SalesTax"
        android:textStyle="bold" android:numeric="integer|decimal"
        android:layout_y="175dip" android:gravity="left"
        android:layout_width="65dip" android:textSize="18dip"
        android:layout_x="100dip" />

    <TextView android:layout_height="wrap_content" android:id="@+id/TextView09"
        android:text="SC Taxable:" android:layout_y="320dip"
        android:layout_x="5dip" android:layout_width="95dip" android:gravity="right" />

    <TextView android:layout_height="wrap_content" android:text="@+id/TextView10"
        android:layout_width="wrap_content" android:id="@+id/ScTaxable"
        android:layout_y="320dip" android:layout_x="110dip" />

    <TextView android:layout_height="wrap_content" android:id="@+id/TextView10"
        android:text="Taxable:" android:layout_y="340dip"
        android:layout_width="95dip" android:layout_x="5dip" android:gravity="right" />

    <TextView android:layout_height="wrap_content" android:text="@+id/TextView11"
        android:layout_width="wrap_content" android:id="@+id/Taxable"
        android:layout_y="340dip" android:layout_x="110dip" />

    <EditText android:layout_height="wrap_content" android:text="9.50"
        android:layout_width="90dip" android:id="@+id/SalesTaxValue"
        android:layout_x="5dip" android:layout_y="430dip" android:numeric="integer|decimal"
        android:selectAllOnFocus="true" />

    <TextView android:layout_height="wrap_content" android:id="@+id/TextView01"
        android:layout_width="wrap_content" android:layout_x="5dip"
        android:text="Sales Tax Value:" android:layout_y="407dip" />
</AbsoluteLayout>
</ScrollView>

这是calculator.java

package com.Eddie.TaxCalculator;

import java.text.NumberFormat;

import com.Eddie.TaxCalculator.R;

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

public class Calculator extends Activity {
    private Button calculateButton;
    private EditText inputField01;
    private EditText inputField02;
    private EditText inputField03;
    private EditText salesTaxValue;
    private double serviceCall;
    private double parts;
    private double labor;
    private double salestaxvalue;


String zero ="0";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        calculateButton = (Button) findViewById(R.id.actionButton);
        calculateButton.setOnClickListener(doSomethingNow);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

        }

    private OnClickListener doSomethingNow = new OnClickListener() {
        public void onClick(View arg0) {
            inputField01 = (EditText) findViewById(R.id.InputNumber01);
            inputField02 = (EditText) findViewById(R.id.InputNumber02);
            inputField03 = (EditText) findViewById(R.id.InputNumber03);
            salesTaxValue = (EditText) findViewById(R.id.SalesTaxValue);


            if (inputField01.getText().toString().length()==0)
                    {
                       inputField01.setText(zero);
                    Toast toast = Toast.makeText(getApplicationContext(), 
                            "Service Call TextBox was empty.",
                            Toast.LENGTH_LONG);
                    toast.show();
                    }       
            if(inputField02.getText().toString().length()==0)
                    {
                       inputField02.setText(zero);
                    Toast toast = Toast.makeText(getApplicationContext(), 
                            "Parts TextBox was empty.",
                            Toast.LENGTH_LONG);
                    toast.show();
                    }   
            if(inputField03.getText().toString().length()==0)
                    {
                       inputField03.setText(zero);
                    Toast toast = Toast.makeText(getApplicationContext(), 
                            "Labor TextBox was empty.",
                            Toast.LENGTH_LONG);
                    toast.show();
                    }
            //service Call is 0
            if (inputField01.getText().toString().equals("0"))
            {
            Toast toast = Toast.makeText(getApplicationContext(), 
                    "Service Call TextBox value is 0.",
                    Toast.LENGTH_SHORT);
            //toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.show();
            }       
            //Parts is 0
                if (inputField02.getText().toString().equals("0"))
            {
               //inputField01.setText("10");
            Toast toast = Toast.makeText(getApplicationContext(), 
                    "Parts TextBox value is 0.",
                    Toast.LENGTH_SHORT);
            toast.show();
            }   
            //Labor is 0
            if (inputField03.getText().toString().equals("0"))
            {
               //inputField01.setText("10");
            Toast toast = Toast.makeText(getApplicationContext(), 
                    "Labor TextBox value is 0.",
                    Toast.LENGTH_SHORT);
            toast.show();
            }   


            serviceCall = Double.parseDouble(inputField01.getText().toString());
            parts = Double.parseDouble(inputField02.getText().toString());
            labor = Double.parseDouble(inputField03.getText().toString());
            salestaxvalue = Double.parseDouble(salesTaxValue.getText().toString());

            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(2);            
            numberFormat.setGroupingUsed(false); 



            TextView totalLabor = (TextView)findViewById(R.id.TotalLabor);
            Double totalLaborResults =  serviceCall +  labor;
            String totalLaborCalcInt = numberFormat.format(totalLaborResults).toString();
            totalLabor.setText (totalLaborCalcInt);

            TextView totalServices = (TextView)findViewById(R.id.TotalServices);
            Double totalServicesResults = (parts + labor);
            String totalServicesCalcInt = numberFormat.format(totalServicesResults);
            totalServices.setText (totalServicesCalcInt);


            TextView servicecallTaxpercent = (TextView)findViewById(R.id.SCTaxPercent);
            Double scTaxPercentResults =  (parts / totalServicesResults);
            String serviceCallPercentCalcInt =  numberFormat.format(scTaxPercentResults).toString();
            servicecallTaxpercent.setText (serviceCallPercentCalcInt);

            TextView scTaxable = (TextView)findViewById(R.id.ScTaxable);
            Double scTaxableResults = (serviceCall * scTaxPercentResults);
            String serviceCallTaxableCalcInt =numberFormat.format (scTaxableResults).toString();
            scTaxable.setText (serviceCallTaxableCalcInt);

            TextView taxable = (TextView)findViewById(R.id.Taxable);
            Double taxableResults = (parts + scTaxableResults);             
            String taxableCalcInt =numberFormat.format (taxableResults).toString();
            taxable.setText (taxableCalcInt);

            TextView salesTax = (TextView)findViewById(R.id.SalesTax);
            Double salesTaxResults = (taxableResults * salestaxvalue) /100;
            String salesTaxCalcInt = numberFormat.format (salesTaxResults).toString();
            salesTax.setText (salesTaxCalcInt);

            TextView textResult = (TextView)findViewById(R.id.Total);
            Double additionResults = (salesTaxResults + parts+ serviceCall +  labor);
            String TotalCalcInt = numberFormat.format(additionResults).toString();
            textResult.setText (TotalCalcInt);

                }

    };

};

在输入1(服务呼叫)中输入数字并在其他两个输入中输入零时,我获得NaN总计。

如果所有输入框均为零值,我也会获得NaN总额,NaN销售税,NaN scTax%textview(sc缺少服务调用) NaN scTaxable,NaN应税。

谁能帮我找到这个,但我已经试了两个星期了。 请帮助。

4 个答案:

答案 0 :(得分:2)

问题是肯定使用零(0)值作为输入...

虽然,我看到你试图用

捕获这样的实例
xxxx.getText().toString().equals("0")

1)这不是一个好方法,检查零...想象如果我输入00000会发生什么......

2)显示Toast后,继续进行计算 ...使用if-else分支逻辑当值为零时避免计算。

要正确检查零...您需要将其解析为数据类型(在您的情况下为Double),然后检查其值....

Goodluck,干杯!

答案 1 :(得分:1)

在浮点数/双打完成时,除以零返回NaN。在分割之前检查分隔符是否为零。

答案 2 :(得分:1)

我的意思是,除以0并不是有意义的,所以......就是不要这样做。

答案 3 :(得分:0)

如果parts为0且labor为0,则totalServicesResults为0,scTaxPercentsResults为无限。