我的问题是我有一个进入意外无限循环的方法,我无法确定原因。该方法应该从用户获取输入并将其分配给三个不同的对象。但是,在循环运行两次并要求用户输入第三个对象的名称后,程序无限循环。我应该指出,我仍然是相对较新的java并且学习编程。
以下是代码(我已经开始发表评论!!!!我相信这与问题有关):
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);
}
}
非常感谢任何帮助或建议:)
干杯
答案 0 :(得分:0)
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.
}
第三个产品是无限循环的原因是因为你应该使用If作为这个语句,同时也可以工作,但是你看到有一个错误是由于你的方法addProduct。如果你改为如果它不是一个无限循环,那么你的产品3将被窃听。
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
注意到您没有检查product3,因此在搜索第3个产品名称时它将始终返回false。
!matesStore.isAProduct(name)=!false = true
堇菜!!你去的时候(真的),一个无限循环!
通过在isAProduct()方法中实现product3.getName()来修复。
您的代码很乱,您应该为产品使用数组,并且可以使用数组进行更好的搜索。
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 if (product.equalsIgnoreCase(product3.getName()))
{
found = true;
}
else
{
counter++;
}
}
return found;
}//End of method