好的,下面的代码(我是新手,请多多包涵)我希望能够向用户提问:

System.out.println("Will the customer be adding any options to the order?");
System.out.println("1. Bed Frame ($39.99)");
System.out.println("2. Pillows ($59.99)");
System.out.println("3. Blankets ($129.99)");
System.out.println("4. No Options added. Enter No");



客户将回答1-4,然后循环浏览,询问客户是否要添加其他内容/您确定不希望添加任何内容。如果没有,则将客户发送到其他班级(更多问题) 。如果是,则再次询问顾客相同的“顾客....”问题。
客户添加让我们说出第1项,然后遍历一个循环,并询问客户是否要添加其他内容/您确定不希望添加任何内容。如果是,则再次询问顾客相同的“顾客....”问题。用户回答完问题后,答案将被保存并打印在writeOutput()行中,以及您添加的产品的名称和价格。


我知道这是一个循环,但我不知道如何实现这种循环。谁能帮助我并填补空白?谢谢。下面是完整的代码

import java.util.Scanner;
public class addons


{

    private int Option;
    private String Bed_Frame, Pillows, Blankets, No_Options_Added;
    private String Name;
    private double Price;
    public void readInput()
    {

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Will the customer be adding any options to the order?");
         System.out.println("1. Bed Frame ($39.99)");
      System.out.println("2. Pillows ($59.99)");
         System.out.println("3. Blankets ($129.99)");
         System.out.println("4. No Options added. Enter No");
        Option = keyboard.nextInt();

       switch (Option)
            {
                case 1:
                   Name = "Bed Frame";
                    Price = 39.99;
                    break;
                case 2:
                   Name = "Pillows";
                   Price = 59.99;
                    break;
                 case 3:
                   Name = "Blankets";
                   Price = 129.99;
                    break;
                 case 4:
                 Name = "No Options Added";
                 System.out.println("No options added");
                   Price = 0;
                    break;
            }
    }

    public void writeOutput()
    {
        System.out.println(Name);
    }


}

最佳答案

只需添加一个while(true) { ... }块,就可以在问题后添加条件-如果答案是“是”,则使用break(不在切换用例的块中!)完成重复循环:

 public void readInput() {

        Scanner keyboard = new Scanner(System.in);
        while (true) {  //ADDED THE WHILE LOOP
           System.out.println("Will the customer be adding any options to the order?");
           System.out.println("1. Bed Frame ($39.99)");
           System.out.println("2. Pillows ($59.99)");
           System.out.println("3. Blankets ($129.99)");
           System.out.println("4. No Options added. Enter No");
           Option = keyboard.nextInt();
           switch (Option)
              {
               case 1:
                 Name = "Bed Frame";
                 Price = 39.99;
                 break;
               case 2:
                 Name = "Pillows";
                 Price = 59.99;
                 break;
               case 3:
                 Name = "Blankets";
                 Price = 129.99;
                 break;
               case 4:
                 Name = "No Options Added";
                 System.out.println("No options added");
                 Price = 0;
                 break;
            }

      //ADDED EXIT POINT WHEN TO EXIT:
      System.out.println("Is that All?");
      //read an answer
      if (*answer was yes*) break;

      } //ADDED A CLOSING BLOCK
}


阅读答案并确定应该满足的条件是一项练习-因为这似乎是一个教程作业,我不想破坏乐趣! :)

10-07 13:20