编码do-while循环?

时间:2016-11-13 21:50:28

标签: java

我正在为我的AP Comp Sci课程完成这项任务,我需要编写一个程序(使用Eclipse),询问用户他们购买的项目数量,我需要用do-while创建代码环。每次循环都必须属于不同的项目。对于这些项目中的每一项,用户都需要输入成本。然后,我需要打印所有项目的总成本和9%销售税后的总额并打印结果。

我试图这样做,但是我无法正确打印出总数,如果我拿出for循环它就无法正常工作。你能给我一些关于如何修复它的指示吗?

public static void main(String[] args) {
    Scanner kbReader = new Scanner(System.in);

    System.out.println("You went shopping on Cyber Monday.");

    do {
        System.out.println("How many items did you purchase?");
        int num = kbReader.nextInt();
        for(int x = 0; x < num;) {
            System.out.println("What did you purchase?");
            String item = kbReader.next();
            System.out.println("How much did it cost?");
            double cost = kbReader.nextDouble();
            break;
        }
        break;
    } while(true);

    double total = cost;
    double totalTax = total + (total * 0.09);
    System.out.println("Total: $" + total);
    System.out.println("Total after tax: $" + totalTax);
}

2 个答案:

答案 0 :(得分:0)

您的实施

您的代码中有三个错误:

  1. 缺少for循环中的x++

  2. 您应该cost声明do-while循环,并且您应该在cost += kbReader.nextDouble();循环中汇总(for)单项费用。

    < / LI>
  3. 您不应该打破for循环(x达到自动中断的目标数字时)。

  4. 此外,在do-whilekbReader.close();

    之后不要忘记关闭扫描仪

    此代码应该有效:

     public static void main(String[] args) {
             Scanner kbReader = new Scanner(System.in);
    
             System.out.println("You went shopping on Cyber Monday.");
             double cost = 0;
             do
             {
             System.out.println("How many items did you purchase?");
             int num = kbReader.nextInt();
             for(int x = 0; x < num;x++)
             {
                 System.out.println("What did you purchase?");
                 String item = kbReader.next();
                 System.out.println("How much did it cost?");
                 cost += kbReader.nextDouble();
             }
             break;
             }while(true);
             kbReader.close();
             double total = cost;
             double totalTax = total + (total * 0.09);
             System.out.println("Total: $" + total);
             System.out.println("Total after tax: $" + totalTax);
         }
    

    正确实施行动

    我想建议您另一个实现,因为我认为如果您使用do-while,您也不应该使用for循环。 这是代码:

    Scanner kbReader = new Scanner(System.in);
    System.out.println("You went shopping on Cyber Monday.");
    double cost = 0;
    int x = 0;
    System.out.println("How many items did you purchase?");
    int num = kbReader.nextInt();
    do
    {
      System.out.println("What did you purchase?");
      String item = kbReader.next();
      System.out.println("How much did it cost?");
      cost += kbReader.nextDouble();
      x++;
    } while(x<num);
    kbReader.close();
    double total = cost;
    double totalTax = total + (total * 0.09);
    System.out.println("Total: $" + total);
    System.out.println("Total after tax: $" + totalTax);
    

答案 1 :(得分:0)

您的代码存在一些问题。第一个是,在它们只运行一次之后它会突破两个循环。

while (true)通常不是一个好主意。我建议你问do while循环上面有多少项,并将计数器设置为0.在while循环体内增加一个计数器,然后在while语句中测试它:

int count = 0;
do {
   <code body>
   count++;
} while (count < num);

此外,您正在尝试访问位于while循环范围内的总变量。它不允许你编译。在while循环上方初始化它,并在循环内增加它(例如total += cost;)。你可以完全摆脱for循环。

试一试!