Java 布尔值总是返回为真?

Java 布尔值总是返回为真?,java,class,Java,Class,所以我正在处理3个类文件。文件1中的一段代码正在调用文件2中的一些代码,检查产品数据库file3是否已满。我希望databaseFull方法返回false,但即使数据库中只有一个项目,它似乎只返回true。 关于为什么会发生这种情况,我能得到一些帮助吗? 编辑:根据要求,我粘贴了我所有的代码 文件1代码: public class StarberksInterface { Scanner console = new Scanner(System.in); public static void

所以我正在处理3个类文件。文件1中的一段代码正在调用文件2中的一些代码,检查产品数据库file3是否已满。我希望databaseFull方法返回false,但即使数据库中只有一个项目,它似乎只返回true。
关于为什么会发生这种情况,我能得到一些帮助吗?
编辑:根据要求,我粘贴了我所有的代码

文件1代码:

public class StarberksInterface
{
Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
    StarberksInterface intFace = new StarberksInterface();
    intFace.run();
}

private void run() 
{
    int option, demandRate, choice;
    double setupCost, unitCost, inventoryCost, sellPrice;
    String productName, newProductName, deleteName;
    Store store = new Store();

    do {
        System.out.println("(1) Input data for one product\n(2) Show data from one product\n(3) Show product replenishment strategy\n(4) Exit");
        option = console.nextInt();
        switch(option)
        {
            case 1: 
                System.out.println("Enter product name (3 to 10 characters): ");
                productName = console.next().toLowerCase();

                while(productName.length()>10 || productName.length()<3 ){
                    System.out.println("Product name length is not valid. Try again.");
                    productName = console.next().toLowerCase();
                }

                if (store.databaseFull()){
                    System.out.println("Database is full:\n(1) Delete a product\n(2) Menu");
                    choice = console.nextInt();
                    if (choice == 1){
                        System.out.println("Enter name of product to delete: ");
                        deleteName = console.next().toLowerCase();
                        store.deleteProduct(deleteName);
                        System.out.println("Product deleted.");
                        break;
                    }
                    else{
                        break;
                    }
                }

                if (store.productExists(productName)){
                    System.out.println("Product already exists:\n(1) Change the product name\n(2) Change product data\n(3) Menu"); 
                    choice = console.nextInt();
                    if (choice == 1){
                        System.out.println("Enter new product name: ");
                        newProductName = console.next().toLowerCase();
                        while(newProductName.length()>10 || newProductName.length()<3 ){
                            System.out.println("Product name length is not valid. Try again.");
                            newProductName = console.next().toLowerCase();
                        }
                        store.changeName(productName, newProductName);

                    }else if (choice == 2){
                        System.out.println("Enter new demand rate: ");
                        demandRate = console.nextInt();
                        demandRate = checkInput(demandRate);

                        System.out.println("Enter new setup cost: ");
                        setupCost = console.nextDouble();
                        setupCost = checkInput(setupCost);

                        System.out.println("Enter new unit cost: ");
                        unitCost = console.nextDouble();
                        unitCost = checkInput(unitCost);

                        System.out.println("Enter new inventory cost: ");
                        inventoryCost = console.nextDouble();
                        inventoryCost = checkInput(inventoryCost);

                        System.out.println("Enter new sell price: ");
                        sellPrice = console.nextDouble();
                        sellPrice = checkInput(sellPrice);

                        System.out.println("Data changed.");

                        store.changeData(productName, demandRate, setupCost, unitCost, inventoryCost, sellPrice);                     
                    } 
                    else{
                        break;
                    }
                }
                else{
                    // user inputs
                    System.out.println("Enter demand rate: ");
                    demandRate = console.nextInt();
                    demandRate = checkInput(demandRate);

                    System.out.println("Enter setup cost: ");
                    setupCost = console.nextDouble();
                    setupCost = checkInput(setupCost);

                    System.out.println("Enter unit cost: ");
                    unitCost = console.nextDouble();
                    unitCost = checkInput(unitCost);

                    System.out.println("Enter inventory cost: ");
                    inventoryCost = console.nextDouble();
                    inventoryCost = checkInput(inventoryCost);

                    System.out.println("Enter sell price: ");
                    sellPrice = console.nextDouble();
                    sellPrice = checkInput(sellPrice);

                    store.addProduct(productName, demandRate, setupCost, unitCost, inventoryCost, sellPrice);
                }
                break;

            case 2:
                System.out.println("Enter product name (3 to 10 characters): ");
                productName = console.next().toLowerCase();

                while(productName.length()>10 || productName.length()<3 ){
                    System.out.println("Product name length is not valid. Try again.");
                    productName = console.next().toLowerCase();
                }

                while (!store.productExists(productName)){
                    System.out.println("This product does not exist, try again.");
                    productName = console.next().toLowerCase();

                    while(productName.length()>10 || productName.length()<3 ){
                        System.out.println("Product name length is not valid. Try again.");
                        productName = console.next().toLowerCase();
                    }
                }

                //at this point, the product entered will exist in the store, so you will be able to show the product details
                showData(store.getProduct(productName));

                break;

            /**case 3: System.out.println
             * 
             * 
             * 
                break;**/

            case 4: break;

            default: System.out.println("invalid option");
        }
    }
    while(option!=4);
}

public void option1(){ 

}

public void option2(){

}

public void option3(){

}

public void option4(){

}

// This function checks integer inputs for negative numbers and returns a message and/or the input.
public int checkInput(int input){

    while(input < 0){
        System.out.println("Cannot input negative number. Try again.");
        input = console.nextInt();
    }

    return input;
}

// This function checks double inputs for negative numbers and returns a message and/or the input.
public double checkInput(double input){

    while(input < 0){
        System.out.println("Cannot input negative number. Try again.");
        input = console.nextDouble();
    }

    return input;
}

private static void showData(Product product)
{
  System.out.println("Name = "+product.getProductName());
  System.out.println("Demand rate = "+product.getDemandRate());
  System.out.println("Setup cost = "+product.getSetupCost());
  System.out.println("Unit cost = "+product.getUnitCost());
  System.out.println("Inventory cost = "+product.getInventoryCost());
  System.out.println("Sell price = "+product.getSellPrice());
}
}

文件3代码:

public class Product
{
private String name;
private int demand;
private double setup, unit, inventory, sell;

public Product()
{
  name = "NO NAME";
  demand = 0;
  setup = 0;
  unit = 0;
  inventory = 0;
  sell = 0;
}

public void setProductName(String productName)
{
    name = productName;
}

public void setDemandRate(int demandRate)
{
    demand = demandRate;
}

public void setSetupCost(Double setupCost)
{
    setup = setupCost;
}

public void setUnitCost(Double unitCost)
{
    unit = unitCost;
}

public void setInventoryCost(Double inventoryCost)
{
    inventory = inventoryCost;
}

public void setSellPrice(Double sellPrice)
{
    sell = sellPrice;
}

public String getProductName()
{
  return name;
}

public int getDemandRate()
{
  return demand;
}

public double getSetupCost()
{
  return setup;
}

public double getUnitCost()
{
  return unit;
}

public double getInventoryCost()
{
  return inventory;
}

public double getSellPrice()
{
  return sell;
}

}

第一次进入
addProduct()
方法时,您正在使用
新产品()设置product1、product2和product3

因此,由于所有这3个属性都引用了有效的产品实例:

product1!=null
返回
true

product2!=null
返回
true

product3!=null
返回
true

然后

(product1!=null&&product2!=null&&product3!=null)
将返回true,因为您总是在商店中创建3种产品。(顺便说一句,您正在创建相同的3次)

试试这个:

public void addProduct(String name, int demand, double setup, double unit, double inventory, double sell ){
    if(product1 == null){
        product1 = new Product();
        //...
    }else if(product2 == null){
        product2 = new Product();
      //...
    }else if(product3 == null){
        product3 = new Product();
      //...
    }
}

调试可能会对您有所帮助。我们不知道你们的DB方面。什么是产品1-2-3??发布整个类。您的Store类将无法编译,因此我非常怀疑该方法返回的是True。您的代码没有明显错误,因此错误可能在其他地方,即填充
product1
product2
product3
的部分。感谢您的回复。应要求,我编辑了我的原始帖子,并包含了所有3个文件的全部代码。我不完全确定你在说什么。您的意思是所有3个产品都已初始化,因此它们不再为空?将我的addProduct的if语句更改为if else可以解决此问题吗?另外,这是否意味着所有3个都接受用户输入的值?
public void addProduct(String name, int demand, double setup, double unit, double inventory, double sell ){
    if(product1 == null){
        product1 = new Product();
        //...
    }

    if(product2 == null){
        product2 = new Product();
        //..
    }

    if(product3 == null){
        product3 = new Product();
        //..
    }
}
public void addProduct(String name, int demand, double setup, double unit, double inventory, double sell ){
    if(product1 == null){
        product1 = new Product();
        //...
    }else if(product2 == null){
        product2 = new Product();
      //...
    }else if(product3 == null){
        product3 = new Product();
      //...
    }
}