有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java销售收据,多选(if/else),while循环,客户更改选择的能力

其他职位似乎都不适合这样做。 我真的在努力制作这个没有错误的程序。我在下面提出了这个问题,但是,如果你运行这个程序,它没有足够的循环,如果你两次选择相同的糖果,那么数学不会添加到之前的选择中

对于第二部分,我需要重新编码以进行调整,以便客户可以根据需要更改当前选择。所以,我需要第一部分完美运行

import java.util.Scanner; //Program uses Scanner
import java.util.Calendar; //Program uses calendar

public class ClairAndreaG005PA2
{//Begin class ClairAndreaG005PA2

 public static void main(String[] args)
  {
   /**Begin method. The customer is asked whether to proceed with a candy purchase.
   * As long as the candy choice is in the proper range, prompt for the quantity,
   * calculate the item total and subtotal; otherwise, print an error message. If it’s 
   * the first item in the purchase, ass it to the sales, a receipt with a $ sign for the
   * item total. If not format the item without a $ sign for the item total 
   * and add it to the sales receipt. Start the process again when the customer wants
   * to add another candy purchase. When the customer is done, the sales tax and the
   * total calculated, the sales receipt is finalized and printed.
   */

    int quantity = 0,
       formatFirstItem = 1,
       choice = 0;


    final double TAX_RATE = .0825; //tax rate declared

    double price = 0,
      itemTotal = 0,
      subtotal = 0,
      taxAmount = 0,
      total = 0;

    char proceed = ' '; //proceed variable for loop

    String candy = new String();

    Calendar dateTime = Calendar.getInstance(); //situational date and time

    Scanner input = new Scanner(System.in);

     String salesReceipt = String.format("%n%nFAIRYTALE SWEETS%n"
                                      + "North Star Mall%n"
                                      + "San Antonio, TX%n"
                                      + "Date: %n", dateTime
                                      + "Time: %n", dateTime);

    //Initiating variables and calculations

    System.out.printf("%nDo you want to proceed with your candy purhase? \'Y\' or \'N\': ");
    proceed = input.nextLine().charAt(0);
    //End Prompt 1

    while(Character.toUpperCase(proceed) == 'Y')
    {
      System.out.printf("%nFAIRYTALE SWEETS"
                    + "%n%n1. Arabian Nights Chocolate Coins - 1 lb. Bag %5s%,7.2f"
                    +"%n2. Beauty and the Beast Lollipops - 1 lb. Bag %,12.2f"
                    +"%n3. Mad Hatter Jelly Beans - 1 lb. Bag %,20.2f"
                    +"%n4. Pinocchio's Candy Cones - Each  %,23.2f"
                    +"%n5. Sleeping Beauty Caramel Apples - Each %,17.2f"
                    +"%n%nEnter your choice: ", "$", 2.25, 2.50, 1.75, 0.75, 1.25);
              choice = input.nextInt();
     //Prompt 2 Candy Menu

     if(choice > 0)
     {if(choice < 6)
        {if(choice == 1)
          {candy = "Arabian Nights Chocolate Coins";
              price = 2.25;
          }
          else
          {if(choice == 2)
            { candy = "Beauty and the Beast Lollipops";
                price = 2.50;
            }
            else
            {if(choice == 3)
              {candy = "Mad Hatter Jelly Beans";
                  price = 1.75;
              }
              else
              {if(choice == 4 )
                {candy = "Pinocchio's Candy Cones";
                    price = 0.75;
                }
                else
                {candy = "Sleeping Beauty Caramel Apples";
                    price = 0.75;
                 }
               }
             }
          }
          System.out.printf("%nQuantity for %s: ", candy);
    quantity = input.nextInt();

    //quantity of selections
     }
     else
      { System.out.println("Invalid candy choice! Try again.");
     }

     //Choices and selections

    itemTotal = quantity * price; //calculation 1
    subtotal += itemTotal; //calculation 2

    System.out.printf("%nWould you like to make another candy purchase? \'Y\' or \'N\': ");
         proceed = input.next().charAt(0);

         if(formatFirstItem == 1 )
         {
           salesReceipt += String.format("%n%s"
                                    + "%n      %d @ $%.2f ex. %-24s $%,10.2f%n", candy,
                                  quantity, price, " ", itemTotal);
           formatFirstItem = 0;
         }
         else
         {
           salesReceipt += String.format("%s"
                                    + "%n      %d @ $%.2f ea. %-25s %,10.2f%n", candy,
                                  quantity, price, " ", itemTotal);

         }
  //End if FormatFIrst Item is 1 or else formatFirstItem NOT 1
    }
    }//End while loop selection


    taxAmount = TAX_RATE * subtotal;// calculation 3
    total = taxAmount + subtotal; // calculation 4

    salesReceipt += String.format("%n%36s %-6s $%,10.2f"
                                + "%n%36s %-7s %,10.2f"
                                + "%n%n%36s %-6s $%,10.2f%n", "SUBTOTAL:   ", " ",
                              subtotal, "TAX @ 8.250%:  ", " ", taxAmount,
                              "TOTAL:  ", " ", total);
    System.out.printf("%s", salesReceipt); 
      }
}

让我知道


共 (0) 个答案