我应该如何重写/修复我的Java代码?我应该使用while或do-while循环吗?

问题描述 投票:0回答:1

该程序的目的是显示笔记本电脑列表,并要求用户购买列出的一台或多台笔记本电脑。

我对在哪里以及如何循环使用此方法感到困惑。如果会做一会儿吗?

A)使用while循环处理给定购买的多台笔记本电脑。使用哨兵控制的循环变量。1)否则将找出笔记本电脑的名称/价格或打印“无效的选择!请重试”消息。3)如果是,则在笔记本电脑选择有效时嵌套,以继续进行其余的处理。

B]如果,则嵌套以仅在实际购买笔记本电脑时打印订单摘要。

C)将printf()与格式说明符一起使用。

D)orderSummary是您已经声明并填充标头信息的String变量。现在,您将购买的每台笔记本电脑(订单项)添加到该标题信息中。使用+ =添加orderSummary中已经存在的内容。

到目前为止,除了使所有用户正确输入所需的循环外,我基本上拥有的所有代码。如果是我的第一个卡住的地方。

import java.util.Calendar;
import java.util.Scanner;

public class RamosS006PA1 {


public static void main(String[] args) { 

char cont='Y';
int qty=0;
int trigger;
double total=0;
double subtotal=0;
double tax=0;
double price;
double lineItem;
String orderSummary=" ";
String laptop;
Scanner input = new Scanner(System.in);
Calendar dateTime = Calendar.getInstance();

//Displays a list of laptops with prices and asked user for their choice. Prompt #1
  System.out.printf("%nTHE TOP LAPTOPS OF %tY"
                    + "%n%n1. %-23s %7s $%,9.2f"
                    + "%n2. %-23s %8s %,9.2f"
                    + "%n3. %-23s %8s %,9.2f"
                    + "%n4. %-23s %8s %,9.2f"
                    + "%n5. %-23s %8s %,9.2f"
                    + "%n%nEnter your choice: ",
                  dateTime,
                  "HP Envy 13", " ", 799.99, 
                  "Asus ZenBook 13 UX333FA", " ", 849.99,
                  "Dell XPS 13", " ", 989.99,
                  "Alienware Area 51-m", " ", 1999.99,
                  "Razer Blade Stealth", " ", 1299.00);

int choice = input.nextInt();

if (choice < 1 || choice > 5) {
  System.out.printf("Invalid choice! Try again.%n%n"); //Prompt #2
  System.out.printf("Enter 'Y' to add a laptop to your purchase or 'N' to exit: "); //Prompt #3
cont = input.nextLine().charAt(0);
}





 //The following if-else prints a $ sign for the first line item
if(trigger == 1)
{
  orderSummary += String.format("%n%, -9d %-30s %8s $%,17.2f",
                                qty, laptop, " ", lineItem);
  trigger = 0;
} //End else for no $ sign



//The following statement prints the order summary of the laptops purchased.
//This should be done when there are no more laptops to process
if (choice > 0)
{
  if (choice < 6) {

    tax = subtotal * .0825;
    total = subtotal + tax;

    orderSummary += String.format("%n%n%34s Subtotal %6s %,17.2f"
                                    + "%n%31s Tax @ 8.25%% %6s %,17.2f"
                                    + "%n%n%37s TOTAL %5s $%,17.2f%n",
                                  " ", " ", subtotal,
                                  " ", " ", tax,
                                  " ", " ", total);
    System.out.printf("%s", orderSummary);

  } //End if valid choice range ends print order summary  
  } //End if valid choice range begins
  }
  }

Screenshot of what output should look like

java if-statement while-loop formatting nested-loops
1个回答
0
投票

您可以将整个(从打印到摘要放到一个循环中),并在显示购买摘要或用户输入要退出系统或循环的内容时中断循环。

int choice = 0;

while(choice >= 0){
//Displays a list of laptops with prices and asked user for their choice. Prompt #1
  System.out.printf("%nTHE TOP LAPTOPS OF %tY"
                    + "%n%n1. %-23s %7s $%,9.2f"
                    + "%n2. %-23s %8s %,9.2f"
                    + "%n3. %-23s %8s %,9.2f"
                    + "%n4. %-23s %8s %,9.2f"
                    + "%n5. %-23s %8s %,9.2f"
                    + "%n%nEnter your choice: ",
                  dateTime,
                  "HP Envy 13", " ", 799.99, 
                  "Asus ZenBook 13 UX333FA", " ", 849.99,
                  "Dell XPS 13", " ", 989.99,
                  "Alienware Area 51-m", " ", 1999.99,
                  "Razer Blade Stealth", " ", 1299.00);

choice = input.nextInt();

if (choice < 1 || choice > 5) {
  System.out.printf("Invalid choice! Try again.%n%n"); //Prompt #2
  System.out.printf("Enter 'Y' to add a laptop to your purchase or 'N' to exit: "); //Prompt #3
cont = input.nextLine().charAt(0);
}

 //The following if-else prints a $ sign for the first line item
if(trigger == 1)
{
  orderSummary += String.format("%n%, -9d %-30s %8s $%,17.2f",
                                qty, laptop, " ", lineItem);
  trigger = 0;
} //End else for no $ sign

//The following statement prints the order summary of the laptops purchased.
//This should be done when there are no more laptops to process
if (choice > 0)
{
  if (choice < 6) 
  {

    tax = subtotal * .0825;
    total = subtotal + tax;

    orderSummary += String.format("%n%n%34s Subtotal %6s %,17.2f"
                                    + "%n%31s Tax @ 8.25%% %6s %,17.2f"
                                    + "%n%n%37s TOTAL %5s $%,17.2f%n",
                                  " ", " ", subtotal,
                                  " ", " ", tax,
                                  " ", " ", total);
    System.out.printf("%s", orderSummary);
    break;

  } //End if valid choice range ends print order summary  
  } //End if valid choice range begins
  }
  }
}
  1. 声明上面的选择并以0初始化
  2. [只要选择> 0运行循环(如果用户输入<0,则循环结束)>>
  3. 成功完成订单后,请中断循环。
© www.soinside.com 2019 - 2024. All rights reserved.