我想在IF条件下为“isChecked”操作提供一个整数。 现在,当我执行时,我的号码错了。
代码:
Button btnWaitress;
EditText etSalaryWaitress;
EditText etSalaryBartender;
//------------
RadioButton rbPercentage;
RadioButton rbShekel;
int HafrashaP;
int HafrashaS;
int etSWNum;
String strHafPer;
String strHafShek;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
btnWaitress =(Button)findViewById(R.id.btnWaitress);
etSalaryWaitress = (EditText) findViewById(R.id.etSalaryWaitress);
etSWNum = Integer.parseInt(etSalaryWaitress.getText().toString());
etSalaryBartender = (EditText) findViewById(R.id.etSalaryBartender);
//-----------
rbPercentage = (RadioButton)findViewById(R.id.rbPercentage);
rbShekel = (RadioButton)findViewById(R.id.rbShekel);
}
public void onClickWait (View v) {
if (rbPercentage.isChecked()) {
HafrashaP = 1 - (etSWNum / 100);
strHafPer= Integer.toString(HafrashaP);
Toast.makeText(settings.this, strHafPer, Toast.LENGTH_SHORT).show();
} else if (rbShekel.isChecked()) {
HafrashaS = - etSWNum;
strHafShek= Integer.toString(HafrashaS);
Toast.makeText(settings.this, strHafShek, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(settings.this, "XXXXXXX", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:1)
您正在执行整数除法运算。考虑整数时总是1/100 = 0。你需要使用double,所以你应该做类似的事情:
ShareViewController
答案 1 :(得分:0)
如果要将整数作为字符串而不是使用toString()
方法,您也可以这样做:
int i=5;
String s="";
s+=5;
这实际上是将'5'与空字符串''连接起来,导致's'的值为:
s="5";
在此之后,您可以使用字符串S进行Toast。
String strHafPer="";
strHafPer+=HafrashaP;
Toast.makeText(settings.this, strHafPer, Toast.LENGTH_SHORT).show();