gpt4 book ai didi

android - 转换摄氏度/华氏度时值错误

转载 作者:行者123 更新时间:2023-11-29 14:41:17 26 4
gpt4 key购买 nike

这是一个基本的单位转换应用程序。

我刚刚完成编码,除华氏温度和摄氏温度计算外,一切正常。公式是对的,但不知道为什么。

这里是Activity.Java

package arirang.unit.converter;

import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.EditText;
import android.widget.TextView;


public class BasicUnitConverterActivity extends Activity {

EditText val3;
String val2;
int val1;
String forResult;
Spinner Type;


/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Spinner spinner1 = (Spinner) findViewById(R.id.Spinner1);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.UnitList, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
}

private String changeFormat(Double dbResult) {
String result;

DecimalFormat format = new DecimalFormat();
result = format.format(dbResult);
return result;
}



public void converter(View View) {


EditText val3 = (EditText) findViewById(R.id.BeforeBox);
String val2 = val3.getText().toString();
if (val2.length() > 0) {

val1 = Integer.parseInt(val2);
Type = (Spinner) findViewById(R.id.Spinner1);

double dbResult = 0;

if (Type.getSelectedItem().toString().equals("meter to inch")) {
dbResult = val1 * 39.3700787;
} else if (Type.getSelectedItem().toString().equals("meter to feet")) {
dbResult = val1 * 3.2808399;
} else if (Type.getSelectedItem().toString().equals("feet to inch")) {
dbResult = val1 * 12;
} else if (Type.getSelectedItem().toString().equals("feet to meter")) {
dbResult = val1 / 3.2808399;
} else if (Type.getSelectedItem().toString().equals("inch to meter")) {
dbResult = val1 / 39.3700787;
} else if (Type.getSelectedItem().toString().equals("inch to feet")) {
dbResult = val1 / 12;
} else if (Type.getSelectedItem().toString().equals("kilogram to pound")) {
dbResult = val1 * 2.20462262;
} else if (Type.getSelectedItem().toString().equals("pound to kilogram")) {
dbResult = val1 / 2.20462262;
} else if (Type.getSelectedItem().toString().equals("Fahrenheit to Celsius")) {
dbResult = (val1 - 32) * (5 / 9);
} else if (Type.getSelectedItem().toString().equals("Celsius to Fahrenheit")) {
dbResult = (val1 * (9 / 5)) + 32;
}

forResult = changeFormat(dbResult);

TextView AfterBox = (TextView) findViewById(R.id.AfterBox);
AfterBox.setText(forResult);
}
}

}

最佳答案

dbResult=(val1 - 32) * (5/9); 应该是 dbResult=(val1 - 32) * (5f/9); 问题是当你写 5/9 时,这意味着你正在除以 int 值,因此结果将为 0(5 小于 9,所以 5/9 为 0。如果它是 10/9,结果将是 1)。通过向其中一个参数添加转换为浮点型,编译器执行所谓的拓宽基元类型转换并将/运算符的两个参数提升为浮点型。现在已经完成了浮点值之间的除法,结果将是您期望的结果。

关于android - 转换摄氏度/华氏度时值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9202386/

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