您好,我正在开发一个Android应用程序,它将帮助学生计算那里的大学CGPA。该应用程序已完成95%,但我有一个小错误,我注意到它可以修复它。
错误是,当我在启动应用程序时第一次在微调器中输入所有值并按“计算”按钮时,它不会产生正确的结果并显示“ NaN”错误。
根据我的研究,当输入为“ 0/0”时出现“ NaN”错误是可以的,但真正的问题是,即使我没有给出输入0/0,它还是在第一次出现NaN错误。当我更改微调器上的输入值并再次按“ CALCULATE”时,则工作正常。
public class MainActivity extends AppCompatActivity {
Spinner spinner,spinner2,spinner3,spinner4,spinner5,spinner6,spinner7,spinner8,spinner9,spinner10,spinner11,spinner12;
TextView result;
Button button1,button2;
int a1=10,a2=10,a3=10,a4=10,a5=10,a6=10;
int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.btn1);
button2=(Button)findViewById(R.id.btn2);
result=(TextView)findViewById(R.id.r1);
spinner=(Spinner)findViewById(R.id.s1);
spinner2=(Spinner)findViewById(R.id.s2);
spinner3=(Spinner)findViewById(R.id.s3);
spinner4=(Spinner)findViewById(R.id.s4);
spinner5=(Spinner)findViewById(R.id.s5);
spinner6=(Spinner)findViewById(R.id.s6);
spinner7=(Spinner)findViewById(R.id.s7);
spinner8=(Spinner)findViewById(R.id.s8);
spinner9=(Spinner)findViewById(R.id.s9);
spinner10=(Spinner)findViewById(R.id.s10);
spinner11=(Spinner)findViewById(R.id.s11);
spinner12=(Spinner)findViewById(R.id.s12);
String[] crdt={"0","1","2","3","4","5"};
String[] grade={"O","A+","A","B+","B","C","P","F"};
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,crdt);
spinner.setAdapter(adapter1);
spinner3.setAdapter(adapter1);
spinner5.setAdapter(adapter1);
spinner7.setAdapter(adapter1);
spinner9.setAdapter(adapter1);
spinner11.setAdapter(adapter1);
ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,grade);
spinner2.setAdapter(adapter2);
spinner4.setAdapter(adapter2);
spinner6.setAdapter(adapter2);
spinner8.setAdapter(adapter2);
spinner10.setAdapter(adapter2);
spinner12.setAdapter(adapter2);
result.setVisibility(View.GONE);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
result.setVisibility(View.VISIBLE);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i)
{
case 0:a1=10; break;
case 1: a1=9;break;
case 2:a1=8;break;
case 3:a1=7;break;
case 4:a1=6;break;
case 5:a1=5;break;
case 6:a1=4;break;
case 7:a1=0;break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i)
{
case 0:a2=10; break;
case 1: a2=9;break;
case 2:a2=8;break;
case 3:a2=7;break;
case 4:a2=6;break;
case 5:a2=5;break;
case 6:a2=4;break;
case 7:a2=0;break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}); spinner6.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i)
{
case 0:a3=10; break;
case 1: a3=9;break;
case 2:a3=8;break;
case 3:a3=7;break;
case 4:a3=6;break;
case 5:a3=5;break;
case 6:a3=4;break;
case 7:a3=0;break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}); spinner8.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i)
{
case 0: a4=10;break;
case 1: a4=9;break;
case 2:a4=8;break;
case 3:a4=7;break;
case 4:a4=6;break;
case 5:a4=5;break;
case 6:a4=4;break;
case 7:a4=0;break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}); spinner10.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i)
{
case 0:a5=10; break;
case 1: a5=9;break;
case 2:a5=8;break;
case 3:a5=7;break;
case 4:a5=6;break;
case 5:a5=5;break;
case 6:a5=4;break;
case 7:a5=0;break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}); spinner12.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i)
{
case 0:a6=10; break;
case 1: a6=9;break;
case 2:a6=8;break;
case 3:a6=7;break;
case 4:a6=6;break;
case 5:a6=5;break;
case 6:a6=4;break;
case 7:a6=0;break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i)
{
case 0:b1=0; break;
case 1:b1=1;break;
case 2:b1=2;break;
case 3:b1=3;break;
case 4:b1=4;break;
case 5:b1=5;break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i)
{
case 0:b2=0; break;
case 1:b2=1;break;
case 2:b2=2;break;
case 3:b2=3;break;
case 4:b2=4;break;
case 5:b2=5;break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});spinner5.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i)
{
case 0:b3=0; break;
case 1:b3=1;break;
case 2:b3=2;break;
case 3:b3=3;break;
case 4:b3=4;break;
case 5:b3=5;break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});spinner7.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i)
{
case 0:b4=0; break;
case 1:b4=1;break;
case 2:b4=2;break;
case 3:b4=3;break;
case 4:b4=4;break;
case 5:b4=5;break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});spinner9.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i)
{
case 0:b5=0; break;
case 1:b5=1;break;
case 2:b5=2;break;
case 3:b5=3;break;
case 4:b5=4;break;
case 5:b5=5;break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});spinner11.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i)
{
case 0:b6=0; break;
case 1:b6=1;break;
case 2:b6=2;break;
case 3:b6=3;break;
case 4:b6=4;break;
case 5:b6=5;break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
double p=(a1*b1+a2*b2+a3*b3+a4*b4+a5*b5+a6*b6);
double q=(b1+b2+b3+b4+b5+b6);
if(p==0 && q==0)
{
result.setText("Result is: 0.0");
}
else {
double r = (p / q);
result.setText("Result is:" + r);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
spinner.setSelection(0);
spinner2.setSelection(0);
spinner3.setSelection(0);
spinner4.setSelection(0);
spinner5.setSelection(0);
spinner6.setSelection(0);
spinner7.setSelection(0);
spinner8.setSelection(0);
spinner9.setSelection(0);
spinner10.setSelection(0);
spinner11.setSelection(0);
spinner12.setSelection(0);
result.setText("Result is: 0.0");
}
});
答案 0 :(得分:0)
问题应该是您要除以0(and you can't)。
这些行的第二个成员没有0支票
if(p==0 && q==0)
{
result.setText("Result is: 0.0");
}
else {
double r = (p / q); //here, q might be 0
result.setText("Result is:" + r);
}
因此,首先确定q为0时该怎么做,然后执行以下操作:
if(p==0 && q==0)
{
result.setText("Result is: 0.0");
}
else {
double r = 0.0;
if(q == 0){
r = p; // or whatever you want
} else{
r = p / q;
}
result.setText("Result is:" + r);
}
...
如果我错过了这个问题,请告诉我!
希望这会有所帮助
答案 1 :(得分:0)
这个问题我看不到任何错误。这个问题是可以理解的。问题出在我的代码(一个简单的)上。 如果可以移动所有
spinner.setItemSelectedListener({
//your codes
{
//switch case and all
}
});
在执行 button1
之前,它应该起作用。