打印所有项目

时间:2016-11-19 06:42:46

标签: java arrays if-statement for-loop

有人可以帮帮我吗?如何打印我将使用的所有项目?提前谢谢你:)

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");
    }

3 个答案:

答案 0 :(得分:1)

如果您还不知道数组,则可以在循环完成后构建字符串中的所有项目以进行打印。一开始:

String items_ordered="";

然后,对于每次购买,当您完成循环时,添加到字符串:

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

最后,打印字符串

System.out.println(items_ordered); 

我认为这就是你要问的......

答案 1 :(得分:0)

这是另一种方式:

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");

答案 2 :(得分:-2)

我认为这可能会对你有所帮助

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);
            }
    }