Answer to Question #269808 in Java | JSP | JSF for nicole

Question #269808
  1. Using the do…while() loop, continuously scan for random integers, but add up only all the positive integers and store the total in one variable.
  2. The loop shall only be terminated when the inputted integer is zero. Afterwards, print out the total of all inputted positive integers.
1
Expert's answer
2021-11-22T09:34:58-0500
import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);

        int input = 0;
        int sum = 0;

        do {
            System.out.print("Enter number: ");
            input = scanner.nextInt();
            if (input > 100) {
                System.out.println("his number is already more than 100");
                break;
            }
            if (sum + input <= 100)
                sum += input;

        } while (sum < 100);

        System.out.println("Sum of Numbers : " + sum);
    }
}

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!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS