在我的Android应用程序中,我使用在EditTexts中输入的值并尝试将答案放入TextView来计算double值。我的代码是这样的:
double scoreDouble;
TextView score;
EditText gpa;
EditText sat;
EditText act;
Button calc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gpa = (EditText) findViewById(R.id.gpa);
String gpaString = gpa.getText().toString();
if(gpaString.equals("")){
gpaString = "0";
}
final double gpaDouble = Double.parseDouble(gpaString);
sat = (EditText) findViewById(R.id.sat);
String satString = sat.getText().toString();
if(satString.equals("")){
satString = "0";
}
final int satInt = Integer.parseInt(satString);
act = (EditText) findViewById(R.id.act);
String actString = act.getText().toString();
if(actString.equals("")){
actString = "0";
}
final int actInt = Integer.parseInt(actString);
score = (TextView) findViewById(R.id.score);
calc = (Button) findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(actInt/36>satInt/2400){
scoreDouble= (0.6*gpaDouble*25)+(0.4*(actInt/36)*100);
String scoreString = Double.toString(scoreDouble);
score.setText("Your score is "+scoreString);
}else{
scoreDouble = (0.6*gpaDouble*25)+(0.4*(satInt/2400)*100);
String scoreString = Double.toString(scoreDouble);
score.setText("Your score is "+scoreString);
}
}
});
}
截至目前,当按下按钮时,TextView会显示:"您的分数为0.0。"我觉得这与我将EditTexts的默认值设置为0的事实有关。在我这样做之前,我收到一个错误,指出NumberFormatException:无效的double:""。如果这是问题,我应该如何解决它。如果那不是问题,它是什么?
答案 0 :(得分:1)
您将actInt和satInt定义为最终变量,并且它们将仅被赋值一次(当执行onCreate方法时)和初始值将为零,因为编辑文本不包含任何内容。
要解决此问题:
将actInt和actInt从局部变量移动到字段变量并删除final关键字。 (我的意思是将这些变量定义为类中的私有变量)并在onclick方法中为变量赋值。
public class test extends Activity {
double scoreDouble;
TextView score;
EditText gpa;
EditText sat;
EditText act;
Button calc;
private int satInt;
private int actInt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gpa = (EditText) findViewById(R.id.gpa);
sat = (EditText) findViewById(R.id.sat);
act = (EditText) findViewById(R.id.act);
score = (TextView) findViewById(R.id.score);
calc = (Button) findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String gpaString = gpa.getText().toString();
if (gpaString.equals("")) {
gpaString = "0";
}
double gpaDouble = Double.parseDouble(gpaString);
String satString = sat.getText().toString();
if (satString.equals("")) {
satString = "0";
}
int satInt = Integer.parseInt(satString);
String actString = act.getText().toString();
if (actString.equals("")) {
actString = "0";
}
int actInt = Integer.parseInt(actString);
if (actInt / 36 > satInt / 2400) {
scoreDouble = (0.6 * gpaDouble * 25)
+ (0.4 * (actInt / 36) * 100);
String scoreString = Double.toString(scoreDouble);
score.setText("Your score is " + scoreString);
} else {
scoreDouble = (0.6 * gpaDouble * 25)
+ (0.4 * (satInt / 2400) * 100);
String scoreString = Double.toString(scoreDouble);
score.setText("Your score is " + scoreString);
}
}
});
}
}
答案 1 :(得分:0)
您正在使用onCreate
方法执行所有操作。因此,在应用程序开始时调用所有代码。此时,您的EditText
为空,您的代码将转到这些部分:
if(gpaString.equals("")){
gpaString = "0";
}
if(actString.equals("")){
actString = "0";
}
if (actString.equals("")) {
actString = "0";
}
这意味着您的值gpaDouble
,actInt
和satInt
等于0.
然后,您正在执行以下操作:
scoreDouble = (0.6*gpaDouble*25)+(0.4*(satInt/36)*100);
和此:
scoreDouble = (0.6*gpaDouble*25)+(0.4*(satInt/2400)*100);
如果值为0,则scoreDouble
值只能等于0.
要解决此问题,请获取EditText
s'您onClick
的{{1}}方法中的文字。