Issue with logic and Loop in Java

前端 未结 4 1762
感动是毒
感动是毒 2020-12-12 06:11

I started coding small program in Java. I wanted to exercise try-catch block, but i did not even come to that part and got stuck on loop part. I know this is very basic loop

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 07:00

    Maybe you can try the following code. In your code , it's not need to use iteration.

    choice = input.nextInt();
    while (choice != 1 && choice != 2) {
        System.out.println("Invalid entry, press 1 or 2");
        choice = input.nextInt();
    }   
    

提交回复
热议问题