2011-07-07 27 views
2

ここで私が取り組んでいるこの初めてのアプリケーションがあります。何らかの理由でこのコードをエミュレータで実行すると、「NaN」という出力が得られます。プログラムは本質的に、いくつかの選択肢(数量と価格を組み合わせたもの)の中で最も低い価格を見つけることを目的としています。私は何が間違っているのか分かりません。何かアドバイス?Android:Javaコード出力 "NaN"

(注意:NaNの出力ではなく、すべてのEditTextフィールドのは、それらの数字を持っている場合にのみ発生)

メインクラス:

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); 


    } 
} 

そして、ここではすべてのクランチングを行い、私のクラスです。

//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が最も低い比率になるので、最良の選択でなければなりません。誰かが私に何をしているのか教えてもらえれば、それは大いに感謝するだろう。

ありがとうございます。

+3

単語を行っているすべてのコードをポストいけないし、私たちは人間の論理コンパイラであることを期待してくださいので、あなたの二重の値はそのexcaptionを避けるために、0.0であってはならないように、チェック条件を入れてください。これを実際に問題のあるものに減らしてください。私には宿題のようにも見えますが、適切であればタグを付けてください。 –

+4

@Jesus Ramos、私が知らないカリキュラムがない限り、Android開発は学校で(少なくとも、まだ)教えられていません。 –

+1

多くの大学には授業があり、私は与えられたときにこれとほぼ同じような課題を見ました。それから私はフロリダにいますが、私はアメリカ全土に友人がいて、その多くにはAndroidを教える学校があります。 –

答えて

14

NaNは、Not a Numberの略で、無効な計算を計算しようとするときに使用される浮動小数点プレースホルダー値です。

最も一般的な操作はゼロ除算です。あなたのコード(ratio[i]=dollarValue[i]/ounce[i];)にちょうど1つの部門があるので、その時点でが0であると思います。 NaN等しくないへの値であるので、あなたは、==を使用してNaNを確認することができません

注意(いなくても自分自身!)。 float/doubleかどうかをチェックするために

はそれぞれFloat.isNaN()Double.isNaN()を使用NaNです。

+0

isNaNメソッドを使って試してみましたが、これは "プリミティブ型doubleでisNaN()を呼び出せません"ということです。助言がありますか? –

+0

@Virtuosoプリミティブ型のメソッドを呼び出すことはできません。そのため、DoubleオブジェクトではisNaN()をdoubleプリミティブではなく呼び出す必要があります。 – Jon7

0

フィールドの1つが空の場合、Double.parseDoubleNumberFormatException(無視する)をスローし、対応する配列エントリはデフォルトの0のままになります。ロジックをoptimize()に従えば、何かを0で割った結果、NaNが返されます。

0

浮動小数点演算に、いくつかの未定義の結果を生成する演算を引き起こす入力パラメータがある場合、NaNが生成されます。

あなたが除算演算にアドバイスの