Answer to Question #289295 in Java | JSP | JSF for Kev

Question #289295

Negative Allergy

by CodeChum Admin

Whole numbers are great, but I think we should also pay attention to decimal numbers, too.


So, how about we make a program that involves a lot of decimals?


Instructions:

  1. Continuously ask for floating point values (decimal numbers) using the do…while() loop, sum them all up, and store the total into one variable.
  2. The loop shall only terminate for the following reasons:
  3. A negative decimal number is inputted (but still included in the total sum)
  4. The total sum reaches 100.0 or more
1
Expert's answer
2022-01-21T01:46:15-0500
package com.task;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc;
        double sum = 0;
        double current;
        boolean isExit = false;
        do {
            System.out.println("Input decimal number");
            sc = new Scanner(System.in);
            current = sc.nextDouble();
            sum = sum + current;
            if (current < 0 || sum >= 100) {
                isExit = true;
            }
        } while (!isExit);
    }

}

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