1

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

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. 客户将回答 1-4,然后通过循环询问客户是否要添加其他任何内容/您确定您不想添加任何内容。如果没有,客户将被发送到另一个班级(更多问题) . 如果是,客户会再次被问到同样的“客户会不会……”问题。
  2. 客户添加让我们说项目 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);
    }

}

4

1 回答 1

2

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

 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
}

阅读答案并确定条件应该作为练习 - 因为它看起来像是一个教程作业,我不想破坏乐趣!:)

于 2012-10-19T23:19:34.270 回答