do-while and while comparison

后端 未结 4 1992
情话喂你
情话喂你 2021-01-05 20:09

do-while:

do
{ 
    i++; 
    ++j;
    System.out.println( i * j );

}
while ((i < 10) && (j*j != 25));

I am learning about do-w

4条回答
  •  庸人自扰
    2021-01-05 21:08

    Do-While loops are pretty useful for cases like these, where you ask the user a question, and the user has to answer it for the program to terminate. For example, this code:

    import java.util.Scanner;
    
    public class DoWhile {
    
        public static void main(String[] args) {
            int answer;
            do {
                System.out.println("What is 5+5?");
                Scanner scan = new Scanner(System.in);
                answer = scan.nextInt();
            } while (answer != 10);
        }
    }
    

提交回复
热议问题