All you must see as code is below.
bSet.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String first = etFirstNumber.getText().toString(); String second = etSecondNumber.getText().toString(); int f = Integer.parseInt(first); int s = Integer.parseInt(second); int calc = 0; int n = 0; if(s < f) { calc = f - s; n = s; }else if(f > s){ calc = s - f; n = f; } int number = n + (int) (Math.random() * calc); String result = String.valueOf(number); tvDescription.setText(result); } });
You can think first
as 35 and second
as 17. I get 0 as result everytime? What is the problem? Can't I use if clauses inside of onclicklistener?
both your conditions are the same:
if(s < f) { calc = f - s; n = s; }else if(f > s){ calc = s - f; n = f; }
so
if(s < f)
and
}else if(f > s){
are the same
change to
}else if(f < s){
Source: Stackoverflow.com