【问题标题】:Variable is not read未读取变量
【发布时间】:2012-05-21 19:48:00
【问题描述】:

未读取 (cancel1 = 2) 布尔值“取消”的代码部分:

while (done !=0){
  pizzaChoice = readInt ("Choose the type of pizza you wish to order");
  double quantity = readInt ("How many " + type[pizzaChoice] + " pizzas do you wish to buy (Maximum pizzas per order is 5)");   

  if(quantity >= max){
    System.out.println("Sorry you cannot order more than five pizzas");


    if ((quantity - max) <= 0){
      double pizzasLeft = max - quantity;

      System.out.println("You can still order " + pizzasLeft + " pizzas");

      pizzasLeft2 = pizzasLeft2 - pizzasLeft;

下一个 if 循环是问题所在

              if (pizzasLeft2 <= 0){
                boolean cancel = true;
                int cancel1 = readInt("Press 2 to cancel your order and start again"); 
                if(cancel1 == 2){
                  cancel = false;
                }

              }
            }
          }

          done= readInt ("Press 0 if you are done or press 1 to make another pizza order");
          double total1 = quantity*price[pizzaChoice];
          total2 = total2 + total1;
        }

【问题讨论】:

  • 名为cancelboolean 仅分配给,从不读取。它的目的是什么?您是否在其他地方有另一个名为 cancelboolean 并且不小心重新声明了它,隐藏了另一个?
  • double 是比萨数量的一个真正奇怪的选择。您的客户可以订购 1.2 个披萨吗?
  • 为什么是布尔型取消??,以及比萨斯Left2 是什么?是进入第一个if 吗?顺便说一句,你声明取消,在'if' 内
  • @Mat 对不起,这只是一个错字

标签: java variables boolean


【解决方案1】:

cancel 相关的代码对我来说似乎是正确的。
我建议您检查具有此值的块。 首先进入if块的条件是否满足?
提示是另一个取消全局变量可能会对其角色产生歧义。

【讨论】:

    【解决方案2】:

    您在抱怨 Eclipse 或您使用的任何 IDE 都在抱怨 cancel 从未使用过,对吗?发生这种情况是因为,即使您分配了取消变量值(true 和 false),您也永远不会使用它,例如:

    if (cancel) {
        System.out.printLn("Thanks for visiting Java Pizza!");
    }
    

    如果您在代码中以某种方式使用它,警告就会消失。或者如果你将它从你的代码中删除,因为它是无用的

    【讨论】:

      猜你喜欢
      • 2013-03-07
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      • 2019-11-30
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      相关资源
      最近更新 更多