多个微调器计算结果不准确
Posted
技术标签:
【中文标题】多个微调器计算结果不准确【英文标题】:multiple spinner caclulation results are not accurate 【发布时间】:2013-12-05 14:49:02 【问题描述】:我是 android 开发的新手,我有一个计算器页面,我需要对从两个微调器中选择的选项进行小计算。我已经使用 switch case 来获得结果。
所有 UI 都按预期出现,但是当我使用按钮计算时,结果并不像预期的那样随机。
谁能指出我犯了什么错误??
活动
public class Calculatorrecon extends Activity implements OnItemSelectedListener
double size, speed, result1, result2;
Button but1;
TextView result;
Spinner shoesize, shoespeed;
String[] size = "Select Size", "100 cm", "80 cm",
"70 cm", "60 cm", "50 cm", "40 cm", "30 cm", ;
String[] speed = "Select Speed", "40", "50", "60", "70", "80", "90", "100", ;
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
result = (TextView) findViewById(R.id.speed);
but1 = (Button) findViewById(R.id.but);
ArrayAdapter<String> speedadapter = new ArrayAdapter<String>(
Calculatorrecon.this, android.R.layout.simple_spinner_item,
speed);
speedadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
shoespeed = (Spinner) findViewById(R.id.speed48);
shoespeed.setAdapter(speedadapter);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
Calculatorrecon.this, android.R.layout.simple_spinner_item,
size);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
shoesize = (Spinner) findViewById(R.id.size);
shoesize.setAdapter(adapter);
shoesize.setOnItemSelectedListener(this);
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
// TODO Auto-generated method stub
int positition = shoesize.getSelectedItemPosition();
switch (positition)
case 0:
break;
case 1:
size = 100;
break;
case 2:
size = 80;
break;
case 3:
size = 70;
break;
case 4:
size = 60;
break;
case 5:
size = 50;
break;
case 6:
size = 40;
break;
case 7:
size = 30;
break;
int speedz = shoespeed.getSelectedItemPosition();
switch (speedz)
case 0:
break;
case 1:
speed = 4;
break;
case 2:
speed = 5;
break;
case 3:
speed = 6;
break;
case 4:
speed = 7;
break;
case 5:
speed = 8;
break;
case 6:
speed = 9;
break;
case 7:
speed = 10;
break;
but1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
calculate();
result.setText(String.format("%.1f", result2) + "");
);
@Override
public void onNothingSelected(AdapterView<?> arg0)
// TODO Auto-generated method stub
private void calculate()
result2 = 0;
double x = size * 8.4563764;
double y = 167.2 * speed;
result2 = y / x;
【问题讨论】:
【参考方案1】:试试这个..
1)您的double
和String[]
变量名称 相同
Spinner shoesize;
Spinner shoespeed;
Button btn;
String[] size = "Select Size", "100 cm", "80 cm", "70 cm", "60 cm",
"50 cm", "40 cm", "30 cm", ;
String[] speed = "Select Speed", "40", "50", "60", "70", "80", "90",
"100", ;
double final_size, final_speed, result2;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
btn = (Button) findViewById(R.id.btn);
shoesize = (Spinner) findViewById(R.id.spinner1);
shoespeed = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> speedadapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, speed);
speedadapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
shoesize.setAdapter(speedadapter);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, size);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
shoespeed.setAdapter(adapter);
shoesize.setOnItemSelectedListener(new OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> arg0, View v, int pos,
long id)
// TODO Auto-generated method stub
int speedz = shoesize.getSelectedItemPosition();
switch (speedz)
case 0:
break;
case 1:
final_speed = 4;
break;
case 2:
final_speed = 5;
break;
case 3:
final_speed = 6;
break;
case 4:
final_speed = 7;
break;
case 5:
final_speed = 8;
break;
case 6:
final_speed = 9;
break;
case 7:
final_speed = 10;
break;
@Override
public void onNothingSelected(AdapterView<?> arg0)
// TODO Auto-generated method stub
);
shoespeed.setOnItemSelectedListener(new OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> arg0, View v, int pos,
long id)
// TODO Auto-generated method stub
int positition = shoespeed.getSelectedItemPosition();
switch (positition)
case 0:
break;
case 1:
final_size = 100;
break;
case 2:
final_size = 80;
break;
case 3:
final_size = 70;
break;
case 4:
final_size = 60;
break;
case 5:
final_size = 50;
break;
case 6:
final_size = 40;
break;
case 7:
final_size = 30;
break;
@Override
public void onNothingSelected(AdapterView<?> arg0)
// TODO Auto-generated method stub
);
btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
result2 = calculate();
Log.v("result", ""+String.format("%.1f", result2));
result.setText(String.format("%.1f", result2) + "");
);
protected double calculate()
double x = final_size * 8.4563764;
double y = 167.2 * final_speed;
double result2 = y / x;
return result2;
【讨论】:
以上是关于多个微调器计算结果不准确的主要内容,如果未能解决你的问题,请参考以下文章
如何获取多个 Spinner View 中的商品数量并计算总价