M
M
Mikhail_1232019-07-10 12:22:13
Java
Mikhail_123, 2019-07-10 12:22:13

Recursion does not work in a simple method, how to fix it?

The task seems to be simple, to make an example of subtracting one random number up to one hundred inclusive, with another random number, and display on the screen, the second number must be less than the first so that there is no negative answer, that's how I did it, all attention to the minus method, which takes the first number, and gives a random second one, checks for compliance with the requirement, if it does not match, then the method calls itself, generating a new second number, and up to "return 123;" in principle, it should not reach, but in practice it reaches half the time, so my recursion does not work. What did I do wrong?

public class Numbers extends AppCompatActivity {
  TextView textView3;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_numbers);
  textView3 = findViewById(R.id.textView3);
  
  int randomDo100 = randomDo100metod();
  String randomDo100String = String.valueOf(randomDo100);
  
  int minus = minus(randomDo100);
  String minusString = String.valueOf(minus);
  
  String textView3String = String.format("%s - %s", randomDo100String, minusString  );
  textView3.setText(textView3String);
  
}
public void onClickTextView(View view) {
}

private int minus(int i) {
  int minusNumber = (int) (Math.random() * 100);
  if (minusNumber<i) {
    return minusNumber;
  } else {
  minus(i);
  }
  return 123;
}


private int randomDo100metod (){
  int randomDo100metod = (int) (Math.random()*100);
  return randomDo100metod;
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-07-10
@Mikhail_123

return minus(i);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question