Android:Java代码输出“NaN” [英] Android: Java code output "NaN"

查看:181
本文介绍了Android:Java代码输出“NaN”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在这里有第一次使用的应用程序。当我出于某种原因在模拟器中运行此代码时,我得到一个NaN输出。该计划主要是为了找到几个选择中的最低价格(数量和价格的总和)。我无法弄清楚我做错了什么。有什么建议?

So here I have this first time app I'm working on. When I run this code in the emulator for some reason I get a "NaN" output. The program essentially is meant to find the lowest price out of several choices (of quantity and price combined). I can't figure out what I'm doing wrong. Any advice?

(注意:只有当并非所有的EditText字段都有数字时才会出现NaN输出)

(Note: The NaN output occurs only when NOT all of the EditText fields have a number in them)

主要类:

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

public class worthit extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button b = (Button)this.findViewById(R.id.btn_calculate);
    b.setOnClickListener(this);
}

public void onClick(View v){

    //Declaring all of our variables that we will use in 
    //future calculations
    EditText price1 = (EditText)this.findViewById(R.id.price1);
    EditText price2 = (EditText)this.findViewById(R.id.price2);
    EditText price3 = (EditText)this.findViewById(R.id.price3);
    EditText price4 = (EditText)this.findViewById(R.id.price4);

    EditText quant1 = (EditText)this.findViewById(R.id.quant1);
    EditText quant2 = (EditText)this.findViewById(R.id.quant2);
    EditText quant3 = (EditText)this.findViewById(R.id.quant3);
    EditText quant4 = (EditText)this.findViewById(R.id.quant4);

    //TextView box used to present the results
    TextView tv = (TextView)this.findViewById(R.id.result);

    //Declaring two arrays of the values from
    //all of our EditText fields
    double[] price = new double[4];
    double[] quantity = new double[4];

    try{
        price[0] = Double.parseDouble(price1.getText().toString());
        price[1] = Double.parseDouble(price2.getText().toString());
        price[2] = Double.parseDouble(price3.getText().toString());
        price[3] = Double.parseDouble(price4.getText().toString());

        quantity[0] = Double.parseDouble(quant1.getText().toString());
        quantity[1] = Double.parseDouble(quant2.getText().toString());
        quantity[2] = Double.parseDouble(quant3.getText().toString());
        quantity[3] = Double.parseDouble(quant4.getText().toString());

        if
    } catch(NumberFormatException nfe) {
        tv.setText("Parsing Error");
    }

    //Creating a Optimize class and using our
    //price and quantity arrays as our parameters
    Calculate optimize = new Calculate(price, quantity);

    //Calling the optimize method to compute the cheapest 
    //choice 
    optimize.optimize();

    //Composing a string to display the results
    String result = "The best choice is the $" +
            optimize.getResultInDollars() + " choice.";

    //Setting the TextView to our result string
    tv.setText(result);


   }
 }

这里是我的班级完成了所有的运算:

And here is my class that does all the crunching:

//Work class used for computing whether
//one choice is cheaper than another given
//a choice of several options af different 
//prices and quantities
//Ex. Coffee- $1-10oz, $1.2-12oz, $1.4-16oz

public class Calculate {
//declaring variables
private double[] dollarValue;
private double[] ounce;
private int indexNumber;        //Index number of the lowest ratio
private double minValue;        //Lowest ratio

private double resultInDollars;
private double resultInOunces;


//class constructor
public Calculate(double[] dol, double[] oun){
    //initializing our variables
    dollarValue=new double[dol.length];
    ounce=new double[oun.length];

    //passing the values from the parameter
    //arrays in our arrays
    for(int i=0;i < dol.length;i++){
        dollarValue[i]=dol[i];
        ounce[i]=oun[i];
    }
}

//Optimize method used to compute the
//cheapest price per quantity
public void optimize(){
    //finding the ratio of the dollar value
    //and the quantity (ounces)
    double[] ratio=new double[dollarValue.length];
    for(int i=0;i<dollarValue.length;i++)
        ratio[i]=dollarValue[i]/ounce[i];

    //finding the smallest value in the ratio
    //array and its location (indexNumber)
    minValue = ratio[0];
    for(int i=1;i < dollarValue.length; i++){
        if(ratio[i] < minValue){
            minValue=ratio[i];
            indexNumber=i;
        }
    }

    //finding the dollar value of the smallest
    //ratio that we found above
    //e.g. most cost effective choice 
    setResultInDollars(minValue*ounce[indexNumber]);
    setResultInOunces(ounce[indexNumber]);
}

public void setResultInDollars(double dollarValueChoiche) {
    this.resultInDollars = dollarValueChoiche;
}

public void setResultInOunces(double resultInOunces) {
    this.resultInOunces = resultInOunces;
}

public double getResultInDollars() {
    return resultInDollars;
}

public double getResultInOunces() {
    return resultInOunces;
}

}

干杯。

编辑:显然我似乎也在某处出现逻辑错误。例如,如果我选择以下价格:1.4,1.6,我选择以下数量(分别)18,20,输出告诉我1.6美元是最好的选择;当你手动计算(1.4 / 18,1 / 6,20)时,你得到1.4的比率最低,因此它必须是最好的选择。如果有人能告诉我我做错了什么会非常感激。

Apparently I also seem to be having a logic error somewhere. For example, if I choose the following prices: 1.4, 1.6, and I choose the following quantities (respectively) 18, 20, The output tells me that the $1.6 is the best choice; when you do the calculation by hand (1.4/18, 1/6,20) you get that 1.4 has the lowest ratio, thus it has to be the best choice. If anyone could tell me what I'm doing wrong that would be much appreciated.

谢谢。

推荐答案

NaN 代表不是数字,是您尝试计算无效计算时使用的浮点占位符值。

NaN stands for Not a Number and is a floating point placeholder value that is used when you try to calculate an invalid computation.

最常见的此类操作是除以零。由于我在你的代码中只看到一个分区( ratio [i] = dollarValue [i] /盎司[i]; ),我猜是盎司[i] 此时 0

The most common such operation is division by zero. Since I see exactly one division in your code (ratio[i]=dollarValue[i]/ounce[i];), I'd guess that ounce[i] is 0 at that point.

请注意,你可以使用 == 检查 NaN ,因为 NaN 不等于任何值(甚至不是自己!)。

Note that you can't check for NaN using ==, because NaN is not equal to any value (not even itself!).

检查是否 float / double NaN 使用 Float.isNaN() Double.isNaN()

To check if a float/double is NaN use Float.isNaN() and Double.isNaN() respectively.

这篇关于Android:Java代码输出“NaN”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆