【问题标题】:loops when user input is wrong and increments when input is right用户输入错误时循环,输入正确时递增
【发布时间】:2015-02-28 11:38:00
【问题描述】:

我的部分作业是创建一个程序,要求用户输入 1-1000 范围内的商品价格。如果用户输入的价格不在该范围内,则程序循环返回以询问相同商品编号的价格(商品编号范围为 1-10)。如果输入价格在范围内,则程序会增加项目编号。并要求用户输入该商品的价格。我就是这样做的。但是,只有第 1 项行为正确。请指教。谢谢

boolean priceinrange = false;       

do {
        System.out.println("Please enter the price of item " + x + ":");
        price = kb.nextDouble();
        if (price>1 && price<1000)
            priceinrange= true; 

    }
    while (!priceinrange);

    do {

        System.out.println("Please enter the price of item:"+ (x+=1) + ":");
        price = kb.nextDouble();
        if (price>1 && price<1000)
            priceinrange= true;

    }   

    while (!priceinrange);

    do {
        System.out.println("Please enter the price of item"+ (x+=1) + ":");
        price = kb.nextDouble();
        if (price>1 && price<1000)
            priceinrange= true;
        else
            System.out.println ("Please enter the price of item" + x +      ":");

    }
    while (!priceinrange);

    do {
        System.out.println("Please enter the price of item " + (x+=1) +  ":");
        price = kb.nextDouble();
        if (price>1 && price<1000)
            priceinrange= true;
        else
            System.out.println ("Please enter the price of item" + x +  ":");
    }
    while (!priceinrange);

    do {
        System.out.println("Please enter the price of item" + (x+=1)+":");
        price = kb.nextDouble();
        if (price>1 && price<1000)
            priceinrange= true;
        else
            System.out.println ("Please enter the price of item" + x +  ":");
    }
    while (!priceinrange);

    do {
        System.out.println("Please enter the price of item" + (x+=1)+ ":");
        price = kb.nextDouble();
        if (price>1 && price<1000)
            priceinrange= true;
        else
            System.out.println ("Please enter the price of item  :");
    }
    while (!priceinrange);

    do {
        System.out.println("Please enter the price of item" + (x+=1) + ":");
        price = kb.nextDouble();
        if (price>1 && price<1000)
            priceinrange= true;
        else
            System.out.println ("Please enter the price of item  :");
    }
    while (!priceinrange);

    do {
        System.out.println("Please enter the price of item" + (x+=1)+ ":");
        price = kb.nextDouble();
        if (price>1 && price<1000)
            priceinrange= true;
        else
            System.out.println ("Please enter the price of item  :");
    }
    while (!priceinrange);

    do {
        System.out.println("Please enter the price of item" + (x+=1) +":");
        price = kb.nextDouble();
        if (price>1 && price<1000)
            priceinrange= true;
        else
            System.out.println ("Please enter the price of item  :");
    }
    while (!priceinrange);

    do {
        System.out.println("Please enter the price of item" +(x+=1)+ ":");
        price = kb.nextDouble();
        if (price>1 && price<1000)
            priceinrange= true;
        else
            System.out.println ("Please enter the price of item  :");
    }

    while (!priceinrange);

【问题讨论】:

  • 对我来说似乎工作正常......
  • 重新命名。循环和迭代的意思是一样的
  • 您可以通过将提示的do-while 移动到您可以调用的方法来降低复杂性。然后,您可以给我们一个简单的for-loop 来提示您输入任意数量的项目...
  • 1-1000 范围是包含还是不包含?您的条件是当前独有的 (2-999)
  • 重新问题文本:“程序迭代项目编号。”我相信你的意思是增加项目编号。如在,无论项目编号是什么,它都会加 1。 0 变为 1,1 变为 2。这是递增的。请介意编辑问题吗?

标签: java do-while


【解决方案1】:

您需要删除所有复制和粘贴代码。额外的代码只会让这更难。

真正的诀窍是在输入错误和正确时循环。只有当您读入 10 个有效价格时,您才会停止循环。你不在乎它最终循环了多少次。你关心你有你的 10 个价格。

如果您是这样想的,那么只需要一个循环即可。 do while (x &lt;= 10)if (price &gt; 1 &amp;&amp; price &lt; 1000) { x += 1;} 将为您带来美好的世界。

您应该能够用不到 25 行代码解决这个问题。我完全没有使用 priceinrange 布尔值就解决了它。

如果这还不够提示,请告诉我。

【讨论】:

  • 我这样做了,我能够有效地使用循环在不到 20 行的时间内解决这部分问题。大帮助!非常感谢!
【解决方案2】:

您需要在每个do { } while() 之前将priceinrange 设置回false。否则即使您输入无效价格,它仍然是true(来自“之前”)。

另一种方法是始终设置它:priceinrange = (price &gt; 1 &amp;&amp; price &lt; 1000),而不是在满足条件时将priceinrange 设置为true

【讨论】:

  • 虽然用 10 个循环当然可以解决这个问题,但这真的值得鼓励吗?
  • 不,但我指出的实际编码错误是回答原始问题的简单逻辑错误。当然,我还没有解决代码中的其他问题。
猜你喜欢
  • 2021-12-20
  • 2021-07-16
  • 2017-02-15
  • 2012-08-30
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 1970-01-01
  • 2016-01-11
相关资源
最近更新 更多