完成对列表的迭代后只需要一个警告。每次迭代都不是一个华纳

时间:2012-04-23 07:51:28

标签: java iteration

我的整个程序如下。

我遇到的问题是我的stdout被这一特定代码行产生的错误消息所淹没。

  for(int x = 0; x < 3; x++)
  {
    for(int xx = 0; xx < 6; xx++)
    {
      if(toppings[x].equalsIgnoreCase(validToppings[xx]))
      {price += 0.75;} else {System.out.println("Error in Pizza class: Attempt to set invalid pizza topping " + toppings[x]);};
    }
  }

我宁愿不重写我花了很多时间完善的代码,但如果我必须使用switch语句或类似的东西来获得简明的错误信息,我会。虽然我认为我目前的方法是最好的。

以下是该课程的考试,

public class TestPizza
{
  public static void main(String args[])
  {
    int x;
    String t1[] = {"Mushrooms", "Onions", ""};
    String t2[] = {"Pepperoni", "Mushrooms", ""};
    String t3[] = {"Pepperoni", "Mushrooms", "Onions"};
    String t4[] = {"Sausage", "", ""};
    Pizza one = new Pizza();
    Pizza two = new Pizza();
    Pizza three = new Pizza();
    Pizza four = new Pizza();

one.setSize(12);
one.addTopping(t1);
one.showValues();

two.setSize(8);
two.addTopping(t2);
two.showValues();

three.setSize(16);
three.addTopping(t3);
three.showValues();

four.setSize(20);
four.addTopping(t4);
four.showValues();

  }
}

这是班级。

// This custom class is used to create Pie objects
// It stores the data about the Pie in four variables:
// size, price, type and baked
// It lets the program that creates the Pie object set these values using four methods:
// setSize, setPrice, setType and bake

public class Pizza
{

  // Declare four variables that can store the values for each pie
  // Each Pie object will have their own, separate copy of these variables 12.
    private int size;
    private double price;
    private boolean baked;
    private int x;
    private int xx;
    private String validToppings[] = {"mushrooms", "pepperonis", "onions", "mushroom", "pepperoni", "onion"};
    private String toppings[] = new String[3];


    // The "constructor" method is called when a new pie
    // object is first created. We use it to set "default" values.
    // Our typical pie is 10 inches, costs $8 and is not baked yet
    // We don't yet know what the pie filling will be
    Pizza()
    {
      size = 999;
      price = 999;
      baked = false;
    }

    // showValues() is a void method that displays the values of the
    // current Pie
    public void showValues()
    {
      System.out.println("Pie Size: " + size);
      for(int x = 0; x < 3; x++) {if(toppings[x] != ("")) {System.out.println("With " + toppings[x]);}};
      System.out.println("Price of Pie: $" + price + "\n\n");
    }

    // getSize() returns the size of the pie
    public int getSize()
    {
      return size;
    }
    // getPrice() returns the price of the pie
    public double getPrice()
    {
      return price;
    }
    // baked() returns whether or not the pie is baked
    public boolean getBaked()
    {
      return baked;
    }
    // setSize() assigns a size to the pie
    public void setSize(int thisSize)
    {
      size = thisSize;
      switch(size)
      {
        case 8: price = 10.00; break;
        case 12: price = 14.00; break;
        case 16: price = 18.00; break;
        default: System.out.println("Error in Pizza class: Attempt to set invalid Pizza size."); break;
      }
    }

    // setPrice() assigns a price to the pie
    public void addTopping(String programToppings[])
    {
      for(int x = 0; x < 3; x++)
      {
        toppings[x] = programToppings[x];
      }
      for(int x = 0; x < 3; x++)
      {
        toppings[x] = toppings[x].toLowerCase();
      }
      for(int x = 0; x < 3; x++)
      {
        for(int xx = 0; xx < 6; xx++)
        {
          if(toppings[x].equalsIgnoreCase(validToppings[xx]))
          {price += 0.75;} else {System.out.println("Error in Pizza class: Attempt to set invalid pizza topping " + toppings[x]);};
        }
      }
    }

  public void bake()
  {
    baked = true;
  };

}

3 个答案:

答案 0 :(得分:1)

您应该使用boolean变量isValid,该变量最初设置为false。找到匹配后,请将其设置为true。完成所有迭代后,如果isValid仍为false,则打印错误消息。

答案 1 :(得分:0)

  boolean error = false;
  for(int x = 0; x < 3; x++)
  {
    for(int xx = 0; xx < 6; xx++)
    {
      if(toppings[x].equalsIgnoreCase(validToppings[xx]))
      {price += 0.75;} else {error = true;};
    }
  }
  if(error){
    System.out.println("Error in Pizza class: Attempt to set invalid pizza topping");
  }

如果你想保留顶部信息,你需要将每个顶部存储在一个列表中(只需在每次错误设置为true时添加),然后在if(error)语句中迭代该列表。

答案 2 :(得分:0)

如果出现错误,您可能不想继续循环。我也会使用Set来进行有效的浇头。

// set globally.
Set<String> validTopppingSet = new HashSet<String>();
for(String t: validToppings) validToppingSet.add(t.toLowerCase());

// when checking the toppings.
for(String topping: topping) {
    if (validToppings.contains(topping.toLowerCase())) {
        price += 0.75;
    } else {
        price = Double.NaN;
        System.out.println("Error in Pizza class: Attempt to set invalid pizza topping: " + topping); // it useful to know what was invalid.
        break;
    }
}
相关问题