【发布时间】:2014-05-08 07:58:18
【问题描述】:
我的问题是我的方法进入了意外的无限循环,但我无法确定原因。该方法应该从用户那里获取输入并将其分配给三个不同的对象。然而,在循环运行两次并要求用户输入第三个对象的名称后,程序无限循环。我应该指出,我对 java 和学习编程还比较陌生。
这是代码(我已经用 !!!! 开始了 cmets,我认为这与问题有关):
private void readInput()
//This method accepts input from the user about product data and sets the values of
//product1, product2 and product3
//Also validates all input so as not to crash the program if the user enters incorrect data
{
String name;
int demandRate, productChoice=1;
final int MAXPRODUCTS=3;
double setupCost, unitCost, inventoryCost, sellingPrice;
Scanner console = new Scanner(System.in);
while (productChoice <= MAXPRODUCTS)
//A loop that makes the user enter data for all three products and then stops after the third entry
{
System.out.println("Please enter the product's name: ");
name = console.next();
matesStore.isAProduct(name); // checks whether the product name the user has entered is unique.
while (matesStore.isAProduct(name))
//If a name that has already been entered is found, the user will be asked to reenter the name
{
System.out.println("That name is already in use. Please choose another: ");
name = console.next();
}
while (!matesStore.isAProduct(name))
//If a name has not been used, the name is added to the product
{
matesStore.addProduct(name); //!!!!I suspect the problem is this line.
}
System.out.println("Please enter the product's demand rate: ");
demandRate = console.nextInt();
System.out.println("Please enter the product's setup cost: ");
setupCost = console.nextDouble();
System.out.println("Please enter the product's unit cost: ");
unitCost = console.nextDouble();
System.out.println("Please enter the product's inventory cost: ");
inventoryCost = console.nextDouble();
System.out.println("Please enter the product's selling price: ");
sellingPrice = console.nextDouble();
matesStore.addData(productChoice, demandRate, setupCost, unitCost, inventoryCost, sellingPrice); //Uses the method from the Store class to set the data values for the products.
productChoice++;
}
while (productChoice > MAXPRODUCTS)
{
System.out.println("The list is now full.");
continueOption();
}
}//End of Method and Interface class
这是来自 Store 类的方法:
public boolean isAProduct(String product)
//Returns true if a name has been found otherwise returns false
{
boolean found = false;
int counter = 0;
while (!found && (counter < MAXNUMBEROFPRODUCTS))
{
if (product.equalsIgnoreCase(product1.getName()))
{
found = true;
}
else if (product.equalsIgnoreCase(product2.getName()))
{
found = true;
}
else
{
counter++;
}
}
return found;
}//End of method
public void addProduct(String product)
//If isAProduct() returns false then the product name
//entered by the user is stored in product1, product2 and product3 respectively
//!!!!I think this is where the problem originates but can't figure out why
{
if (numberOfProducts == 0)
//!!!!numberOfProducts has been declared as private int numberOfProducts=0;
//I tried declaring numberOfProducts as a variable within addProduct()
//but that just set the value to 0 each time and so only the name for product1 was set
{
product1.setName(product);
numberOfProducts++;
}
else if (numberOfProducts == 1)
{
product2.setName(product);
numberOfProducts++;
}
else if (numberOfProducts == 2)
{
product3.setName(product);
}
else
{
System.exit(0);
}
}
任何帮助或建议将不胜感激:)
干杯
【问题讨论】:
-
第一个循环条件是 productChoice MAXPRODUCTS。是在 continueOption 中减少 productChoice 还是这是一个/问题?
-
尝试插入一些调试语句,而不是试图猜测它在哪里循环。
-
当您的程序进入无限循环时,究竟打印了什么?
-
抱歉,我不明白为什么需要减少 productChoice。它从 1 开始,因此小于 3 并进入循环,然后在循环结束时增加 1,因此它变为 2 并再次进入循环,依此类推,直到值达到 4,在这种情况下循环结束并且通知用户列表已满,然后 continueOption() 将用户带回主界面。完成所有操作后,我是否应该将 productChoice 减回 0?对不起,我只是有点困惑:)
-
@Zeeshan 没什么,我输入第三个对象的名称,程序就停止接受输入。但它并没有停止,我必须手动停止程序,这就是为什么我认为它是一个无限循环。
标签: java infinite-loop