Hi, I'm having a problem with a code which is supposed to read in values for the number of boxes of cookies sold by girl scouts and compute the total money collected for each troop. It must work for more than just a few troops, so I have a while loop, but I can't get the program to allow the troop name to be entered after the first run through.
Here is my code:
package lab5; import java.util.Scanner; public class Scouts { /** * @param args */ public static void main(String[] args) { Scanner in = new Scanner (System.in); int w = 0; int d = 0; int p = 0; double total = 0; System.out.println("Cookie Sales Report for Young Scouts of America"); System.out.println("Enter the troop name (or exit to quit): "); String t = in.nextLine(); while (!t.equals("exit")) { System.out.println("Enter number of boxes of Chocolate Mint Wafers: "); w = in.nextInt(); System.out.println("Enter number of boxes of Peanut Delights: "); d = in.nextInt(); System.out.println("Enter number of boxes of Coconut Plops: "); p = in.nextInt(); total = w*3+d*3.5+p*2.25; System.out.println("The total collected by Troop " + t + " is $" + total); System.out.println("Enter the troop name (or exit to quit): "); t = in.nextLine(); } System.out.println("The total collected overall is $" + total + "."); } }
This is what I get when I run the program:
Cookie Sales Report for Young Scouts of America
Enter the troop name (or exit to quit):
Tulips
Enter number of boxes of Chocolate Mint Wafers:
1
Enter number of boxes of Peanut Delights:
2
Enter number of boxes of Coconut Plops:
3
The total collected by Troop Tulips is $16.75
Enter the troop name (or exit to quit):
Enter number of boxes of Chocolate Mint Wafers:
As you can see, the change of the String t is skipped. An additional troop cannot be named and the loop can't be exited.
I may just be approaching this all wrong, but either way, I would greatly appreciate any help.
Also, once I figure this out, I need to print information for each troop. I have no idea how I'm going to accomplish that without setting a limit on the number of troops that can be entered and assigning each to a variable, so a hint on that would be great, too.
Thanks!