Is there a command in java to make the program go back to the beginning of a loop

后端 未结 5 771
独厮守ぢ
独厮守ぢ 2021-01-22 12:22

I am trying to make a typing adventure kind of game in java, however i need a command at least similar to the one in the title, here is the code

import java.util         


        
5条回答
  •  [愿得一人]
    2021-01-22 12:31

    Your question makes an incorrect assumption. A switch() statement is not a loop. It's shorthand for a bunch of if/else-if statements like this:

    if(sc1.equals("Eat coconut")) {
        System.out.println("You ate the coconut and got poisoned");
        System.out.println("You died...");
    }
    else if(sc1.equals("Throw coconut")) {
    //and so on
    

    To do what you want, you will need an actual while() or for() loop. (The links will take you to Java tutorials explaining both constructions.)

提交回复
热议问题