Home>

I want to display the good morning for the numbers entered in java using the do-while statement.
Input value: How can I hide it when 0 is specified?

Error message
----- ----- ----- ----- -----
INFO: Good morning is displayed for the number of numbers entered.
----- ----- ----- ----- -----
ENTER: 0
----- ----- ----- ----- -----
RESULT:
Good morning
----- ----- ----- ----- -----
Applicable source code

`` `java
import java.util.Scanner;

public class DoWhile {
public static void main (String [] args) {

Scanner scanner = new Scanner (System.in);

System.out.println ("----- ----- ----- ----- -----");
System.out.println ("INFO: Display good morning for the number entered.&Quot;);
System.out.println ("----- ----- ----- ----- -----");
System.out.print ("ENTER:");

int userInputTimes = scanner.nextInt ();

System.out.println ("----- ----- ----- ----- -----");
System.out.println ("RESULT:");

int count = 0;
do {
System.out.println ("Good morning");
count ++;
} while (count<userInputTimes);

System.out.println ("----- ----- ----- ----- -----");
scanner.close ();
}
}

  • Answer # 1

    do {processing;} while (continuation conditional expression);is intended to "determine the continuation condition after processing once". If you don't want to do anything at all, use the syntax ofwhile (continuation conditional expression) {processing;}.

  • Answer # 2

    .do ~ whilestatement is always executed once, so if you want to hide it when you enter 0
    For example, it needs to be adjusted with aifstatement.

    import java.util. *;
    public class Main {
        public static void main (String [] args) {
           Scanner sc = new Scanner (System.in);
           System.out.println ("INFO: Display good morning for the number of numbers entered.");
           int n = Integer.parseInt (sc.nextLine ());
           int i = 0;
           if (n>0) {
               do {
                  System.out.println ("Good morning");
                  i ++;
               } while (n>i);
           }
        }
    }