Redo the method instruction

Asked

Viewed 89 times

1

I would like to know how I do to redo the instruction of a method until the user cancels.

I have the following method, for example:

public static void method(){
  int i = 1;
  i += 1;
  String choise;

  println("Exit?");
  choise = scan.next();

  if(choise.equals("y")){
    System.Exit(0);
  } else if(choise.equals("n")){
    /* ???? */
  } else{
    ...
  }
}

I imagine I could use a loop, while for example, and run the instruction until Choise is "n". But I would like to know if it was possible to do it differently without using a loop.

  • There are many alternatives. What is the action to have is against action?

  • So. The idea is that whenever the user type "N" he redoes i += 1; assuming the state of the variable is 1.

3 answers

7


We can solve the same problem iterativa (used a repeat loop) or through chamadas recursivas a uma função.

To recursividade is the definition of a sub-rotina (função or método) that can invoke itself.

Knowing that we have more than one possible solution we should always seek the best for each situation when using recursion or loops, in your case the ideal solution is through a laço de repetição:

while (choise.equals("y")){

    println("Exit?");
    choise = scan.next();

    if(choise.equals("y")){
        System.Exit(0);
    } else if(choise.equals("n")){
        /* ... */
    } else {
        /* ... */
    }

}

5

Try this:

while (choise.equals("y")){
    println("Exit?");
    choise = scan.next();

    if(choise.equals("y")){
        System.Exit(0);
    } else if(choise.equals("n")){
        /* ???? */
    } else{
        ...
    }
}

Or you can use another recursive method, which calls itself if it’s not Y.

  • 2

    Recursion to a loop normal doesn’t sound like a good idea. You can still incur a classic StackOverflow Exception!

  • It was as I said Kalebemtos; I could use a loop, but the title of studies and knowledge, I would like to know another possible way.

  • 1

    This is the correct solution. It would only take away the indication of the recursive that is almost never the solution, in this case it is not even.

-2

It doesn’t just use reference

else if(choise.equals("n")){
      method();
    } else{
  • Perfect. I imagined something like this, but I couldn’t test.

  • 2

    This solution should not be used, this will cause problem in the call stack.

  • 1

    Recursiveness to the problem in question ? Not a good idea at all, as I commented in the other answer

  • 5

    @Joseliz guy we can always answer what we are asked only, but the ideial is to seek excellence in our answers, we must understand why of the question and guide what is best

  • 1

    "whether it was possible to do this otherwise than by using a loop". It’s all possible but you not only didn’t explain your code, you didn’t give any indication whether it would be good or bad to do so, just like you didn’t show how it would be done with while that the question code does not have. It is convenient to bear in mind that the answers in the SOPT serve as reference for future readers so we should try as much as possible to instill good practices and give truly good solutions.

  • Like I said. I know how to use loops. And regardless of what function is doing, until pq in the case was a quick example I did, I wanted to know another way to do the repeat without using a loop. Now that I have seen that recursive functions can be used, I will study what it is and when to use it. That’s enough for me!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.