为学生出勤率创建一个Android应用程序。 当我运行我的应用程序时,我得到的结果为NaN。我将两个浮点值从主活动传递给活动A1。但在我的应用程序中,“e2”字段显示值“NaN”。直到现在我找不到我的错误。它必须在划分浮点值。
我的代码中有什么错误?
MainActivity.java
package shafir.arraytest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sort(View v) {
EditText e1, e2, e3, e4, e5;
char ch = 0;
e1 = (EditText) findViewById(R.id.text1);
e2 = (EditText) findViewById(R.id.text2);
e3 = (EditText) findViewById(R.id.text3);
e4 = (EditText) findViewById(R.id.text4);
e5 = (EditText) findViewById(R.id.text5);
Button b1;
b1 = (Button) findViewById(R.id.sort);
int i1, i2, i3,c1 = 0, c2 = 0;
float tot,att;
i1 = Integer.parseInt(e1.getText().toString());
i2 = Integer.parseInt(e2.getText().toString());
i3 = Integer.parseInt(e3.getText().toString());
tot=Float.parseFloat(e4.getText().toString());
att=Float.parseFloat(e5.getText().toString());
if ((i1 == i2) && (i1 == i3)) {
c1 = i1;
c2 = i1;
}
if ((i1 == i2) || (i1 > i2) && (i1 == i3) || (i1 > i3)) {
c1 = i1;
ch = '1';
}
if ((i2 == i1) || (i2 > i1) && (i2 == i3) || (i2 > i3)) {
c1 = i2;
ch = '2';
}
if ((i3 == i1) || (i3 > i1) && (i3 == i2) || (i3 > i2)) {
c1 = i3;
ch = '3';
}
switch (ch) {
case '1':
if (i2 > i3) {
c2 = i2;
} else {
c2 = i3;
}
break;
case '2':
if (i1 > i3) {
c2 = i1;
} else {
c2 = i3;
}
break;
case '3':
if (i1 > i2) {
c2 = i1;
} else {
c2 = i2;
}
break;
}
Intent in1 = new Intent(this, A1.class);
in1.putExtra("c1", c1);
in1.putExtra("c2", c2);
in1.putExtra("tot", c1);
in1.putExtra("att", c2);
startActivity(in1);
}
}
`
----------
----------
`
**This is my Activity A1:**
package shafir.arraytest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
public class A1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a1);
edit();
}
public void edit() {
EditText e1, e2, e3;
float ex, patt, internal,tempp,tot1,att1;
int g1, g2,tempsum, matt = 0;
e1 = (EditText) findViewById(R.id.great1);
e2 = (EditText) findViewById(R.id.great2);
e3 = (EditText) findViewById(R.id.inter);
Intent in2 = getIntent();
g1 = in2.getIntExtra("c1", 0);
g2 = in2.getIntExtra("c2", 0);
tot1 = in2.getFloatExtra("tot", 0);
att1 = in2.getFloatExtra("att", 0);
tempsum=g1+g2;
ex = (float) (tempsum*0.075);
tempp=att1/tot1;
patt = (float)(att1 / tot1) * 100;
if ((patt > 90) && (patt <= 100)) {
matt = 5;
} else if ((patt > 80) && (patt <= 90)) {
matt = 4;
} else if ((patt > 70) && (patt <= 80)) {
matt = 3;
} else if ((patt > 60) && (patt <= 70)) {
matt = 2;
} else if ((patt >= 50) && (patt <= 60)) {
matt = 1;
} else {
matt = 0;
}
internal = ex + matt;
e1.setText(Float.toString(ex));
e2.setText(Float.toString(tempp));
e3.setText(Float.toString(internal));
}
}
答案 0 :(得分:2)
我的猜测是tot1为0然后你得到NaN,因为你不能除以0。 如果不是这样,请添加一些打印语句,看看tempp转向NaN的位置。
答案 1 :(得分:0)
我发现了我的错误:我在传递主要活动的值时犯了一个错误。
我为float使用了相同的变量名c1
和c2
。现在它已经清除,我的应用程序运行正常。