2016-11-19 5 views
-3

Kann mir bitte jemand helfen? Wie kann ich alle Artikel drucken, die ich nutzen werde? Vielen Dank im Voraus :)Drucken aller Artikel

public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     String choice; 
     String want = ""; 
     double price, total = 0, payment, change; 

     System.out.println("Beauty Zone Salon"); 
     System.out.println("Welcome Customer"); 
     System.out.println("Hair Cut......100"); 
     System.out.println("Manicure......200"); 
     System.out.println("Pedicure......300"); 

     do { 
      System.out.print("Enter your choice: "); 
      choice = input.nextLine(); 
      if (choice.equalsIgnoreCase("Haircut")) { 
       System.out.println("You choose Haircut"); 
       price = 100; 
       total = total + price; 
       System.out.print("Do you want to do another service?"); 
       want = input.nextLine(); 

      } else if (choice.equalsIgnoreCase("Manicure")) { 
       System.out.println("You choose Manicure"); 
       price = 200; 
       total = total + price; 
       System.out.print("Do you want to do another service?"); 
       want = input.nextLine(); 

      } else if (choice.equalsIgnoreCase("Pedicure")) { 
       System.out.println("You choose Pedicure"); 
       price = 300; 
       total = total + price; 
       System.out.print("Do you want to do another service?"); 
       want = input.nextLine(); 
      } 
     } while (want.equalsIgnoreCase("Y")); 
     System.out.println("ITEMS\n"); 
     System.out.println("Your total due is " + total); 
     System.out.print("Enter amount of your payment:"); 
     payment = input.nextDouble(); 
     change = payment - total; 

     System.out.println("Your change is: " + change); 
     System.out.println("Goodbye"); 
    } 
+1

Können Sie Ihren Code in einem lesbaren Format haben. – randominstanceOfLivingThing

Antwort

1

Wenn Sie nicht wissen, Arrays noch, können Sie alle Elemente in einem String aufbauen zu gedruckt werden, nachdem die Schleife abgeschlossen ist. Zu Beginn:

String items_ordered=""; 

Dann für jeden Kauf, wenn Sie durch die Schleife gehen, fügen Sie die Zeichenfolge:

items_ordered+="Haircut... 100\n" 

Am Ende, drucken Sie die Zeichenfolge

System.out.println(items_ordered); 

I denke, das ist, was Sie fragen ...

+0

vielen Dank :) –

-2

Ich denke, das kann Ihnen helfen

public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     String choice; 
     String want = ""; 
     double price, total = 0, payment, change; 
      List<String> totalItems = new ArrayList<>();  
     System.out.println("Beauty Zone Salon"); 
     System.out.println("Welcome Customer"); 
     System.out.println("Hair Cut......100"); 
     System.out.println("Manicure......200"); 
     System.out.println("Pedicure......300"); 

     do { 
      System.out.print("Enter your choice: "); 
      choice = input.nextLine(); 
      if (choice.equalsIgnoreCase("Haircut")) { 
       System.out.println("You choose Haircut"); 
       price = 100; 
          totalItems.add("Harcut"); 
       total = total + price; 
       System.out.print("Do you want to do another service?"); 
       want = input.nextLine(); 

      } else if (choice.equalsIgnoreCase("Manicure")) { 
       System.out.println("You choose Manicure"); 
       price = 200; 
          totalItems.add("Manicure"); 
       total = total + price; 
       System.out.print("Do you want to do another service?"); 
       want = input.nextLine(); 

      } else if (choice.equalsIgnoreCase("Pedicure")) { 
       System.out.println("You choose Pedicure"); 
       price = 300; 
          totalItems.add("Preducure"); 
       total = total + price; 
       System.out.print("Do you want to do another service?"); 
       want = input.nextLine(); 
      } 
     } while (want.equalsIgnoreCase("Y")); 
     System.out.println("ITEMS\n"); 
     System.out.println("Your total due is " + total); 
     System.out.print("Enter amount of your payment:"); 
     payment = input.nextDouble(); 
     change = payment - total; 

     System.out.println("Your change is: " + change); 
     System.out.println("Goodbye"); 

      System.out.println("Total Items:"); 
      for(String item: totalItems){ 
       System.out.println(item); 
      } 
    } 
+0

In welcher Weise wird es helfen? Was hast du geändert und warum? Was war falsch im ursprünglichen Code und wie haben deine Änderungen das behoben? Am allerwenigsten markieren Sie die Abschnitte des Codes, die Sie mit Kommentaren geändert haben. Eine Nur-Code-Antwort ohne diese wichtigen Details ist meistens nutzlos ... was wahrscheinlich die Antwort (en) erklärt, die Ihre Antwort erhalten hat. Weitere Informationen finden Sie unter "[Antwort]". –

0

Und hier ist noch eine weitere Möglichkeit:

Scanner input = new Scanner (System.in); 

// Pick a Service by way of Service Number entry 
// rather than entering the service name. 
System.out.println("Beauty Zone Salon"); 
System.out.println("Welcome Customer"); 
System.out.println("1) Hair Cut......$100.00"); 
System.out.println("2) Manicure......$200.00"); 
System.out.println("3) Pedicure......$300.00"); 

String want = "";  // String variable to hold the 'Do You Want More Service' answer. 
String items = "";  // String variable to hold delimited items selected. 
int choice = 0;   // Integer varaible to hold the service selected. 
double total = 0.0;  // Double variable to hold the total cost of all selected services. 
double payment = 0.0; // Double variable to hold the payment supplied. 
double change = 0.0; // Double variable to hold the monetary amount to be returned to customer. 
while (want.equalsIgnoreCase("y") || want.equals("")) { 
    System.out.print("\nEnter your choice (1 to 3 - 0 to cancel): "); 
    choice = input.nextInt(); 
    input.nextLine(); // Handle the \r\n which isn't handled by Scanner.nextInt() 
    // If 0 is supplied then User wants to cancel all. 
    if (choice == 0) { break; } 

    // Hair Cut selected... 
    if(choice == 1){ 
     // Make sure Hair Cut hasn't already been selected. 
     if (items.contains("Hair Cut")) { 
      System.out.println("You have already selected a Hair Cut!"); 
      continue; 
     } 
     System.out.println("You choose Hair Cut (cost $100.00)"); 
     if (items.equals("")) { items+= "Hair Cut ($100.00)"; } 
     else { items+= ";Hair Cut ($100.00)"; } 
     total+= 100.00; 
    } 
    // Manicure selected... 
    else if(choice == 2){ 
     // Make sure Manicure hasn't already been selected. 
     if (items.contains("Manicure")) { 
      System.out.println("You have already selected a Manicure!"); 
      continue; 
     } 
     System.out.println("You choose Manicure (cost $200.00)"); 
     if (items.equals("")) { items+= "Manicure ($200.00)"; } 
     else { items+= ";Manicure ($200.00)"; } 
     total+= 200.00; 
    } 
    // Pedicure selected... 
    else if(choice == 3){ 
     // Make sure Pedicure hasn't already been selected. 
     if (items.contains("Pedicure")) { 
      System.out.println("You have already selected a Pedicure!"); 
      continue; 
     } 
     System.out.println("You choose Pedicure (cost $300.00)"); 
     if (items.equals("")) { items+= "Pedicure ($300.00)"; } 
     else { items+= ";Pedicure ($300.00)"; } 
     total+= 300.00; 
    } 

    // If all items in the list have been selected by the 
    // customer then jump out and provide the total due. 
    if (items.split(";").length == 3) { break; } 
    want = ""; 
    while (want.equals("") || (!want.equalsIgnoreCase("y") && !want.equalsIgnoreCase("n"))) { 
     System.out.print("\nDo you want to do another service? (y/n)"); 
     want = input.nextLine(); 
    } 
} 
if (!items.equals("")) { 
    System.out.println("\n=============================="); 
    System.out.println("ITEMS SELECTED:"); 
    String[] selectedItems = items.split(";"); 
    for (int i = 0; i < selectedItems.length; i++) { 
     System.out.println(selectedItems[i]); 
    } 
    System.out.println("Your total due is: $"+total); 
    System.out.println("------------------------------"); 

    while (payment < total) { 
     System.out.print("\nEnter amount of your payment (0 to cancel):"); 
     payment=input.nextDouble(); input.nextLine(); 
     if (payment == 0) { break; } 
     if (payment < total) { 
      System.out.print("Payment must be $" + total + " or more."); 
     } 
    } 
    if (payment != 0) { 
     change = payment - total; 
     System.out.println("Change back from your $" + payment + " payment is: $" + change); 
      } 
    else { System.out.println("SERVICE CANCELED!"); } 
} 
else { System.out.println("SERVICE CANCELED!"); } 

System.out.println("\nThank you for your business, Goodbye"); 
+0

Es ist schwer, den ganzen Code für die Kommentare zu finden, die deine Antwort erklären ... besonders, wenn sich so viel vom Original geändert hat. (Ich muss weiter scrollen, um zu sehen, ob die Unterschiede echte Codeänderungen oder nur Kommentare und Formatierungen sind.) Ihre Antwort wäre viel besser mit ein paar Zeilen Text, um die Änderungen, die Sie vorgenommen haben, und warum. –

Verwandte Themen