how can i use java scanner in while loop

前端 未结 4 766
爱一瞬间的悲伤
爱一瞬间的悲伤 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 16:01

    You are trying to redeclare the variable inside the loop. You only want to give the existing variable a different value:

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

    This is just an assignment rather than a declaration.

提交回复
热议问题