gpt4 book ai didi

java - 不知道如何摆脱 NaN

转载 作者:行者123 更新时间:2023-12-01 19:20:23 25 4
gpt4 key购买 nike

好的,我正在构建这个计算 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>

这是计算器.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 Total。

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

任何人都可以帮我找到这个,但我已经尝试了两周了。请帮忙。

最佳答案

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

尽管如此,我看到你试图用

捕获此类实例
xxxx.getText().toString().equals("0")

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

2) 显示 Toast 后,您可以继续进行计算 无论如何...使用 if- else 分支逻辑以避免值为零时的计算。

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

祝你好运,干杯!

关于java - 不知道如何摆脱 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4544068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com