how can i use java scanner in while loop

前端 未结 4 764
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 15:35

This is what I have so far:

int question = sc.nextInt(); 

while (question!=1){

    System.out.println(\"Enter The Correct Number ! \");

    int question =         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-21 15:59

    Reuse the question variable instead of redeclaring it.

    int question = sc.nextInt(); 
    while (question != 1) {
        System.out.println("Enter The Correct Number ! ");
        question = sc.nextInt(); // ask again
    }
    

提交回复
热议问题