Java意外无限循环

Java意外无限循环,java,infinite-loop,Java,Infinite Loop,我的问题是,我有一个方法进入了一个意外的无限循环,我不知道为什么。该方法应该从用户那里获取输入并将其分配给三个不同的对象。但是,在循环运行两次并要求用户输入第三个对象的名称后,程序无限循环。我应该指出的是,我对java还是比较陌生的,一般来说,我都在学习编程 这是我开始评论的代码!!!!我认为这与问题有关: private void readInput() //This method accepts input from the user about product data and s

我的问题是,我有一个方法进入了一个意外的无限循环,我不知道为什么。该方法应该从用户那里获取输入并将其分配给三个不同的对象。但是,在循环运行两次并要求用户输入第三个对象的名称后,程序无限循环。我应该指出的是,我对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);
    }
} 
如有任何帮助或建议,将不胜感激:

干杯

第三个产品是无限循环的原因是,您应该在这个语句中使用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,因此在搜索第三个产品名称时,它将始终返回false

!!matesStore.isAProductname=!假=真

中提琴!!你来了,它是whiletrue,一个无限循环

通过在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      

第一个循环条件是productChoice MAXPRODUCTS。productChoice是在continueOption中减少的还是问题所在?请尝试插入一些调试语句,而不是尝试猜测它在哪里循环。当您的程序进入无限循环时,究竟打印的是什么?抱歉,我不明白为什么需要减少productChoice。它从1开始,因此小于3并进入循环,然后在循环结束时增加1,使其变为2并再次进入循环,等等,直到值达到4,在这种情况下,循环结束并通知用户列表已满,然后继续操作将用户带回主界面。完成所有这些之后,我是否应该将productChoice减回到0?对不起,我只是有点困惑:@Zeeshan没什么,我输入了第三个对象的名称,程序就停止接受输入。但是它没有停止,我必须手动停止程序,这就是为什么我认为它是一个无限循环。非常感谢。我可以用你的建议来解决我的问题:另外,这个程序是针对一个uni项目的,它特别要求我们不要使用数组。我们刚刚开始学习它们,下一个任务要求我们修改这个程序以使用数组haha@user3610513我明白了,难怪你不使用数组。数组将解决问题,并使代码更具可读性。这很奇怪,因为我在学习方法之前就学会了数组。我认为在方法和类之前先了解数据类型更重要。然而,当我试图在我的大脑中调试你的代码时,阅读你的代码让我在我的工作场所感觉更加清醒!哈哈,是的,这可能有道理,但我想这取决于讲师。那很好!我觉得我只是在问这么简单而且可能是盲目的问题,哈哈:@mrbige无论如何我注意到你的matesStore.addProductname不需要while循环;虽然matesStore.isAProductname{}循环可以删除,因为之前您已经验证了它是否正确,然后才允许用户跳出循环。是的,我现在已经更改了它:
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