Question #82149

Create a program that satisfy the following requirements:
1. Ask user to enter integer numbers, one at a time.
2. If the number is -1, stop entering number.
3. Print out the total number that is greater than 18.


For example:
The following is screen shot when user enters the sentence “computer science is great”.

enter an integer:
13
enter an integer:
2
enter an integer:
21
enter an integer:
40
enter an integer:
-1
Total number that is greater than 18: 2
1

Expert's answer

2018-10-19T09:11:09-0400

Task

Create a program that satisfy the following requirements:

1. Ask user to enter integer numbers, one at a time.

2. If the number is -1, stop entering number.

3. Print out the total number that is greater than 18.

For example:

The following is screen shot when user enters the sentence "computer science is great".


enter an integer:
13
enter an integer:
2
enter an integer:
21
enter an integer:
40
enter an integer:
-1


Total number that is greater than 18: 2

Solution

import java.util.Scanner;
public class Question82149 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = 0;
        int x = 0;
        while (x != -1) {
            System.out.println("enter an integer:");
            x = scanner.nextInt();
            if (x > 18)
                count++;
        }
        System.out.println("Total number that is greater than 18: " + count);
    }
}
"C:\Program Files\Java\jdk1.0.0_172\bin\java.exe" ...
enter an integer:
12
enter an integer:
13
enter an integer:
15
enter an integer:
19
enter an integer:
22
enter an integer:
13
enter an integer:
-1
Total number that is greater than 18: 2
Process finished with exit code 0

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS