gpt4 book ai didi

java - 在我的计算器应用程序中使用运算符时出现 "NaN"错误

转载 作者:行者123 更新时间:2023-12-01 16:24:18 25 4
gpt4 key购买 nike

所以我一直在根据网上的教程开发计算器应用程序。到目前为止一切都很好,但每次我尝试使用运算符(operator)按钮时,我似乎无法找出导致应用程序中出现“NaN”错误的原因。我将附上代码和应用程​​序的屏幕截图。抱歉,如果我含糊不清,我是编码新手,只是想学习。

我的MainActivity.java

package com.example.calculator_prjct;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.app.Activity;
import com.example.calculator_prjct.databinding.ActivityMainBinding;
import android.os.Bundle;
import android.view.View;
import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

private ActivityMainBinding binding;
private static final char ADDITION = '+';
private static final char SUBTRACTION = '-';
private static final char MULTIPLICATION = '*';
private static final char DIVISION = '/';

private char CURRENT_ACTION;

private double valueOne = Double.NaN;
private double valueTwo;

private DecimalFormat decimalFormat;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

decimalFormat = new DecimalFormat("#.##########");

final ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

binding.buttonDot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + ".");
}
});

binding.buttonZero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "0");
}
});

binding.buttonOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "1");
}
});

binding.buttonTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "2");
}
});

binding.buttonThree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "3");
}
});

binding.buttonFour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "4");
}
});

binding.buttonFive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "5");
}
});

binding.buttonSix.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "6");
}
});

binding.buttonSeven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "7");
}
});

binding.buttonEight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "8");
}
});

binding.buttonNine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "9");
}
});


binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = ADDITION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});

binding.buttonSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = SUBTRACTION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "-");
binding.editText.setText(null);
}
});

binding.buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = MULTIPLICATION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "*");
binding.editText.setText(null);
}
});

binding.buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = DIVISION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "/");
binding.editText.setText(null);
}
});

binding.buttonEquals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
binding.infoTextView.setText(binding.infoTextView.getText().toString() +
decimalFormat.format(valueTwo) + " = " + decimalFormat.format(valueOne));
valueOne = Double.NaN;
CURRENT_ACTION = '0';
}
});

binding.buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (binding.editText.getText().length() > 0) {
CharSequence currentText = binding.editText.getText();
binding.editText.setText(currentText.subSequence(0, currentText.length() - 1));
} else {
valueOne = Double.NaN;
valueTwo = Double.NaN;
binding.editText.setText("");
binding.infoTextView.setText("");
}
}
});
}

private void computeCalculation() {
if (!Double.isNaN(valueOne)) {
valueTwo = Double.parseDouble(binding.editText.getText().toString());
binding.editText.setText(null);

if (CURRENT_ACTION == ADDITION)
valueOne = this.valueOne + valueTwo;
else if (CURRENT_ACTION == SUBTRACTION)
valueOne = this.valueOne - valueTwo;
else if (CURRENT_ACTION == MULTIPLICATION)
valueOne = this.valueOne * valueTwo;
else if (CURRENT_ACTION == DIVISION)
valueOne = this.valueOne / valueTwo;
} else {
try {
valueOne = Double.parseDouble(binding.editText.getText().toString());
} catch (Exception e) {
}
}

}

}

我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/infoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:textSize="30sp" />

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/infoTextView"
android:enabled="false"
android:gravity="bottom"
android:lines="2"
android:maxLines="2"
android:textSize="20sp" />

<Button
android:id="@+id/buttonSeven"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:text="7"
android:textSize="20sp" />

<Button
android:id="@+id/buttonFour"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="1dp"
android:layout_marginLeft="-87dp"
android:layout_marginTop="47dp"
android:layout_toRightOf="@id/buttonSeven"
android:text="4"
android:textSize="20sp" />

<Button
android:id="@+id/buttonFive"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_marginLeft="1dp"
android:layout_marginTop="47dp"
android:layout_toRightOf="@id/buttonSeven"
android:text="5"
android:textSize="20sp" />

<Button
android:id="@+id/buttonSix"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_marginLeft="89dp"
android:layout_marginTop="46dp"
android:layout_toRightOf="@id/buttonSeven"
android:text="6"
android:textSize="20sp" />

<Button
android:id="@+id/buttonOne"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="-1dp"
android:layout_marginLeft="-89dp"
android:layout_marginTop="96dp"
android:layout_toRightOf="@id/buttonSeven"
android:text="1"
android:textSize="20sp" />

<Button
android:id="@+id/buttonTwo"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_marginLeft="-1dp"
android:layout_marginTop="95dp"
android:layout_toRightOf="@id/buttonSeven"
android:text="2"
android:textSize="20sp" />

<Button
android:id="@+id/buttonThree"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_marginLeft="88dp"
android:layout_marginTop="97dp"
android:layout_toRightOf="@id/buttonSeven"
android:text="3"
android:textSize="20sp" />

<Button
android:id="@+id/buttonDot"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="2dp"
android:layout_marginLeft="-86dp"
android:layout_marginTop="144dp"
android:layout_toRightOf="@id/buttonSeven"
android:text="."
android:textSize="20sp" />

<Button
android:id="@+id/buttonZero"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="90dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="144dp"
android:layout_toRightOf="@id/buttonSeven"
android:text="0"
android:textSize="20sp" />

<Button
android:id="@+id/buttonEquals"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="179dp"
android:layout_marginLeft="91dp"
android:layout_marginTop="145dp"
android:layout_toRightOf="@id/buttonSeven"
android:text="="
android:textSize="20sp" />


<Button
android:id="@+id/buttonClear"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="265dp"
android:layout_marginLeft="177dp"
android:layout_marginTop="197dp"
android:layout_toRightOf="@id/buttonSeven"
android:text="C"
android:textSize="20sp" />

<Button
android:id="@+id/buttonEight"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_toRightOf="@id/buttonSeven"
android:text="8"
android:textSize="20sp" />

<Button
android:id="@+id/buttonNine"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_toRightOf="@id/buttonEight"
android:text="9"
android:textSize="20sp" />

<Button
android:id="@+id/buttonDivide"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonNine"
android:layout_toRightOf="@id/buttonNine"
android:text="/"
android:textSize="20sp" />

<Button
android:id="@+id/buttonMultiply"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonDivide"
android:layout_marginLeft="-88dp"
android:layout_marginTop="48dp"
android:layout_toRightOf="@id/buttonDivide"
android:text="*"
android:textSize="20sp" />

<Button
android:id="@+id/buttonSubtract"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonMultiply"
android:layout_marginLeft="-86dp"
android:layout_marginTop="53dp"
android:layout_toRightOf="@id/buttonMultiply"
android:text="-"
android:textSize="20sp" />

<Button
android:id="@+id/buttonAdd"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonSubtract"
android:layout_marginLeft="-89dp"
android:layout_marginTop="49dp"
android:layout_toRightOf="@id/buttonSubtract"
android:text="+"
android:textSize="20sp" />

</RelativeLayout>
</layout>

实际错误的屏幕截图 "NaN Error on all operator buttons"

最佳答案

这是因为您没有检查所有操作按钮各自的onClick方法中的EditText是否为空。将这样的代码添加到所有操作符按钮:

binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = ADDITION;

//if editText was empty then valueOne would still be NaN
//and infoTextView will show NaN+ with this line of code

binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});

完整 Activity 代码:

public class MainActivity extends AppCompatActivity {

private ActivityMainBinding binding;
private static final char ADDITION = '+';
private static final char SUBTRACTION = '-';
private static final char MULTIPLICATION = '*';
private static final char DIVISION = '/';

private char CURRENT_ACTION;

private double valueOne = Double.NaN;
private double valueTwo;

private DecimalFormat decimalFormat;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);


decimalFormat = new DecimalFormat("#.##########");

binding.buttonDot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + ".");
}
});

binding.buttonZero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "0");
}
});

binding.buttonOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "1");
}
});

binding.buttonTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "2");
}
});

binding.buttonThree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "3");
}
});

binding.buttonFour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "4");
}
});

binding.buttonFive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "5");
}
});

binding.buttonSix.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "6");
}
});

binding.buttonSeven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "7");
}
});

binding.buttonEight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "8");
}
});

binding.buttonNine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "9");
}
});


binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = ADDITION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});

binding.buttonSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = SUBTRACTION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "-");
binding.editText.setText(null);
}
});

binding.buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = MULTIPLICATION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "*");
binding.editText.setText(null);
}
});

binding.buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = DIVISION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "/");
binding.editText.setText(null);
}
});

binding.buttonEquals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
binding.infoTextView.setText(binding.infoTextView.getText().toString() +
decimalFormat.format(valueTwo) + " = " + decimalFormat.format(valueOne));
valueOne = Double.NaN;
CURRENT_ACTION = '0';
}
});

binding.buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (binding.editText.getText().length() > 0) {
CharSequence currentText = binding.editText.getText();
binding.editText.setText(currentText.subSequence(0, currentText.length() - 1));
} else {
valueOne = Double.NaN;
valueTwo = Double.NaN;
binding.editText.setText("");
binding.infoTextView.setText("");
}
}
});
}

private void computeCalculation() {
if (!Double.isNaN(valueOne)) {
valueTwo = Double.parseDouble(binding.editText.getText().toString());
binding.editText.setText(null);

if (CURRENT_ACTION == ADDITION)
valueOne = this.valueOne + valueTwo;
else if (CURRENT_ACTION == SUBTRACTION)
valueOne = this.valueOne - valueTwo;
else if (CURRENT_ACTION == MULTIPLICATION)
valueOne = this.valueOne * valueTwo;
else if (CURRENT_ACTION == DIVISION)
valueOne = this.valueOne / valueTwo;
} else {
try {
valueOne = Double.parseDouble(binding.editText.getText().toString());
} catch (Exception e) {
}
}

}
}

关于java - 在我的计算器应用程序中使用运算符时出现 "NaN"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62185050/

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