Answer to Question #324739 in Java | JSP | JSF for Holly

Question #324739

Negative Allergy (JAVA)

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

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

Input

Multiple lines containing float number on each.

1.1
1.2
1.3
1.4
-1.0

Output

A line containing a float number with two decimal places.

4.00
1
Expert's answer
2022-04-10T06:27:47-0400


import java.util.Scanner;


public class App {


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


		float number;
		float sum=0;
	    do{
			number=keyboard.nextFloat();
			sum+=number;
		}while(sum<100 && number>0);
	    
	    System.out.printf("%.2f\n\n",sum);
		


		keyboard.close();
	}


}

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