【问题标题】:Result appearing as infinity结果显示为无穷大
【发布时间】:2016-02-17 20:38:25
【问题描述】:

我的计算结果显示为无穷大时存在一些问题。

基本上,有一个用于用户输入的 Edittext 和两个单选按钮。克和盎司。

当用户输入他们的编辑文本/服务大小时,我需要根据单选按钮选择运行计算。

如果选择了 Grams 单选按钮,我需要 textview 来显示“1000/Servingsize”的结果

如果选择了 Ounces 单选按钮,我需要文本视图来显示“(1000 / (servingsize * 28.3495)) 的结果

我已经为我最近的两次尝试输入了代码,我也尝试了其他代码,但是昨晚已经很晚了,我这辈子都记不起来了。

您能提供的任何帮助将不胜感激。

干杯,杰斯。

工作部分(结果如预期):

package com.example.jess.dogfeedingguide;

import...

public class kCals extends AppCompatActivity {
     EditText firstNumber;
     EditText secondNumber;
     EditText thirdNumber;
     EditText servingsize;
     TextView addResult;
     TextView addResult2;
     Button btnAdd;
     Button btnAdd2;
     RadioButton grradio, ozradio;
     double num1, num2, num3, serve, servefinalg, servefinaloz, serve2, ozinkg, sum;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_k_cals);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    this.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    servingsize = (EditText) findViewById(R.id.txtNumber4);
    firstNumber = (EditText) findViewById(R.id.txtNumber1);
    secondNumber = (EditText) findViewById(R.id.txtNumber2);
    thirdNumber = (EditText) findViewById(R.id.txtNumber3);
    btnAdd = (Button) findViewById(R.id.btnAdd);
    addResult = (TextView) findViewById(R.id.txtResult);
    grradio = (RadioButton) findViewById(R.id.radioButton1);
    ozradio = (RadioButton) findViewById(R.id.radioButton2);
    btnAdd2 = (Button) findViewById(R.id.btnAdd2);
    addResult2 = (TextView) findViewById(R.id.txtResult2);
    btnAdd.setOnClickListener(new View.OnClickListener()

// Perform kCals Per Serve Calculation //

        // Check for Blank Values //

    public double parseDouble(String doubleText) {
        if (TextUtils.isEmpty(doubleText)) {
            return 0.00;
        }
        try {
            return Double.parseDouble(doubleText);
        } catch (NumberFormatException e) {
            return 0.00;
        }
    }

        // Calculation //

        public void onClick(View v) {

            num1 = parseDouble(firstNumber.getText().toString());
            num2 = parseDouble(secondNumber.getText().toString());
            num3 = parseDouble(thirdNumber.getText().toString());
            sum = (num1 * 4) + (num2 * 4) + (num3 * 9);
            addResult.setText(String.format("%.2f", sum));

        }
    });
}

非工作部分(从上面的部分开始,结果无穷大):

// Perform kCals Per Kilogram Calculation //

        // Check for Blank Values //

    public double parseDouble(String doubleText) {
        if (TextUtils.isEmpty(doubleText)) {
            return 0.00;
        }
        try {
            return Double.parseDouble(doubleText);
        } catch (NumberFormatException e) {
            return 0.00;
        }
    }

    // Calculation //
    public void onRadioButtonClicked(View view) {

    int kg = 1000;
    ozinkg = 28.3495; 
    serve = parseDouble(servingsize.getText().toString());
    serve2 = serve * ozinkg;
    double servefinalg = (double) kg / serve;
    double servefinaloz = (double) kg / serve2;
    String stservefinalg = Double.toString(servefinalg);
    String stservefinaloz = Double.toString(servefinaloz);

    boolean checked = ((RadioButton) view).isChecked();

    switch (view.getId()) {
            case R.id.radioButton1:
                    if (checked) {

                            addResult2.setText(stservefinalg);
                            addResult2.setEnabled(true);

                    } else {
                            addResult2.setEnabled(false);
                    }
                    break;
            case R.id.radioButton2:
                    if (checked) {
                            addResult2.setText(stservefinaloz);
                            addResult2.setEnabled(true);

                    } else {
                            addResult2.setEnabled(false);
                    }
                    break;}
        }
     }
  }

我也尝试了以下方法:

// Perform kCals Per Kilogram Calculation //

// Check for Blank Values //

public double parseDouble(String doubleText) {
    if (TextUtils.isEmpty(doubleText)) {
        return 0.00;
    }

    try {
        return Double.parseDouble(doubleText);
    } catch (NumberFormatException e) {
        // do something if invalid number maybe also return 0?
        return 0.00;
    }
}

// Calculation //

public void onRadioButtonClicked(View view) {

    ozinkg = 28.3495;
    serve = parseDouble(servingsize.getText().toString());
    servefinalg = 1000.00 / serve;
    servefinaloz = 1000.00 / (serve * 28.3495);

    boolean checked = ((RadioButton) view).isChecked();

    switch (view.getId()) {
        case R.id.radioButton1:
            if (checked) {
                addResult2.setText(String.format("%.2f", servefinalg));
                addResult2.setEnabled(true);

            } else {
                addResult2.setEnabled(false);
            }
            break;

        case R.id.radioButton2:
            if (checked) {
                addResult2.setText(String.format("%.2f", servefinaloz));
                addResult2.setEnabled(true);

            } else {
                addResult2.setEnabled(false);
            }
            break;
        }
    }
}

【问题讨论】:

  • 您确定 parseDouble 返回的值大于 0 吗?
  • 基本上我在适用的编辑文本中输入“200”之类的值。

标签: java android infinity


【解决方案1】:

serve = parseDouble(servingsize.getText().toString()); 很可能是导致问题的原因。

获得无穷大的主要方法是除以零。

public double parseDouble(String doubleText) {
if (TextUtils.isEmpty(doubleText)) {
    return 0.00;
}

try {
    return Double.parseDouble(doubleText);
} catch (NumberFormatException e) {
    // do something if invalid number maybe also return 0?
    return 0.00;
}
}

在抛出异常或输入为空的情况下返回零。

因为文本是一串字符,所以你需要一些东西来解析成双精度/整数。

在它为空或抛出异常的情况下(这是正在发生的事情,因为它是一个字符串,而不是一个数字),它返回零。

servefinalg = 1000.00 / serve; 是被服务除,由serve = parseDouble(servingsize.getText().toString()); 设置。

您的 parseDouble 方法将其分配为零,然后除以零。

我的解决方案是改用乘法,将除数更改为分数,因此如果输入无效,则将其设置为零而不是无穷大。

【讨论】:

    猜你喜欢
    • 2022-08-12
    • 2012-12-17
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2019-03-19
    • 2023-03-16
    • 2010-10-23
    相关资源
    最近更新 更多