Results 1 to 2 of 2
Thread: Help with a loop-like problem
- 10-13-2009, 11:14 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
Help with a loop-like problem
Hi all, first post here.
I had a question about a program i was thinking of writing but am having trouble with finishing.
the thing is i get stuck hereInput contains several money amounts. Each represents a donation. The ned is denoted by a negative amount. The program will print each amount on a separate line, with a dollar sign. At the end it will print a blank line, total donations, and the average. Note that the amounts as well as the average are printed to two decimal places, rounded.
The output for an input such as 24.95 64.50 77 -89.90 will be
$ 24.95
$ 64.50
$ 77.00
Total from 3 donations = $ 166.45
Average = $ 55.48
i know i havent started writing the code for the total and average, but i first need to know how i get this stupid loop to work with the total and average.Java Code:import java.util.*; public class Prog5 { public static void main(String args[]) { double donations, total, average; Scanner sea = new Scanner(System.in); System.out.print("Donations please: "); do donations = sea.nextDouble(); while(donations >= 0); System.out.printf("%.2d", donations); } }
any help please
- 10-14-2009, 01:15 AM #2
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
nevermind, i think i got it
Java Code:import java.util.*; import java.text.DecimalFormat; public class Prog5 { public static void main(String args[]) { double donations, average, total; int i; String input; char repeat; DecimalFormat formatter = new DecimalFormat("#,##0.00"); Scanner sea = new Scanner(System.in); i = 0; total = 0; do { System.out.print("\nDonation: "); donations = sea.nextDouble(); if(donations < 0) donations = 0; sea.nextLine(); i++; total += donations; average = total / i; System.out.println("\nAnother donation? "); System.out.print("Enter Y for yes or N for no: "); input = sea.nextLine(); repeat = input.charAt(0); } while(repeat == 'Y' || repeat == 'y'); System.out.println("\nTotal from " + i + " donations = $ " + total); System.out.println("Average = $ " + formatter.format(average)); } }
Similar Threads
-
if else loop problem
By Ms.Ranjan in forum New To JavaReplies: 12Last Post: 04-25-2009, 09:30 AM -
Some while loop problem need help
By shaggyoo7 in forum New To JavaReplies: 4Last Post: 01-14-2009, 07:16 PM -
Loop Problem
By jralexander in forum New To JavaReplies: 4Last Post: 12-02-2008, 07:08 AM -
Problem to use different for loop to add up
By matt_well in forum New To JavaReplies: 6Last Post: 08-03-2008, 10:24 PM -
For loop problem
By mcal in forum New To JavaReplies: 32Last Post: 01-25-2008, 03:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks