简体   繁体   English

如何在两个方法之间使用一个公共变量?

[英]How can I have a common variable between two methods?

boolean exists;
public void searchProduct(Vector <Stock> temp){
    Scanner sc = new Scanner (System.in);
    sc.useDelimiter ("\n");
    exists = false;
    System.out.println ("Enter Product Name to Search for: ");
    String n = sc.next();
    System.out.println ();
    Stock s = null;
    if(temp.size() == 0)
        System.out.println ("Database is Empty! Please Add a Product.");
    else if (!n.matches(valn))
        System.out.println ("Invalid Product.Please Try Again.");
    else{
        for(int i = 0; i < temp.size(); i ++){
            s = temp.elementAt(i);
            if(s.getName().equalsIgnoreCase(n)){
                System.out.println(s.ToString());
                System.out.println ("Location: " + (i + 1));
                exists = true;
            }
        }
        if(exists != true)
            System.out.println ("Product Not Found!");
        System.out.println ("---------------------------------------------"); 
    } 
}

public void delProduct(Vector <Stock> temp){
    Scanner sc = new Scanner (System.in);
    if(temp.size() == 0)
        System.out.println ("Database is Empty! Please Add a Product.");
    else{
        sc.useDelimiter("\n");
        Stock s = new Stock();
        int choice = 0;
        int i = 0;
        boolean quit = false;
        boolean valid = true; 
        s.searchProduct(temp);
        if(exists == true){ 
            do{
                try{
                    System.out.println ("Enter Location of Product to delete: ");
                    i = sc.nextInt();
                    temp.removeElementAt(i-1);
                    System.out.println ();
                    System.out.println ("Product was Deleted Succesfully!");
                    System.out.println ("--------------------------------"); 
                    valid = false;
                }catch(InputMismatchException ime){
                    sc.next();
                    System.out.println ("Invalid Location.");
                }catch(ArrayIndexOutOfBoundsException a){
                    System.out.println ("Location does not Exist!");
                }
            }while(valid); 
        }
    }
}

As it is, everything works perfectly fine. 实际上,一切都很好。 But the thing is, that if the user enters a product name to delete that does not exist in the vector, he is still expected to enter the location to delete. 但事实是,如果用户输入的product name不存在于矢量中,则仍然希望他输入要删除的位置。 (Obviously, it is doing what it is supposed to do). (显然,它正在执行应该执行的操作)。 I have tried declaring different boolean variables that are set to true if the search item is found, and the delete procedure is only executed if the boolean is true, but somehow the variable is always false in the delete method. 我尝试过声明如果找到搜索项就将其设置为true的其他布尔变量,并且仅在布尔值为true时才执行删除过程,但是在delete方法中,该变量始终为false。 Any ideas please? 有什么想法吗? Btw, sorry if I make silly mistakes, I am still new to Java. 顺便说一句,如果我犯了愚蠢的错误,对不起,我还是Java的新手。

The problem is that, when you do s.searchProduct(temp) you are expecting that exists to be true if the product is found but it is not the case. 问题是,当您执行s.searchProduct(temp) exists ,如果找到了产品,您期望它exists ,但事实并非如此。 Because while you are doing s.searchProduct(temp) , you are calling method searchProduct(temp) from another Stock object that you called s . 因为在执行s.searchProduct(temp) ,您是从另一个名为s Stock对象中调用方法searchProduct(temp) Therefore if the product exists in temp, then exists of s will be true, so s.exists will be true. 因此,如果乘积以temp exists ,则s exists将为真,因此s.exists将为真。 However, what you want to be true is this.exists . 但是,您想要真实的是this.exists For this, you have to replace s.searchProduct(temp) line with this.searchProduct(temp) or searchProduct(temp) . 为此,您必须用this.searchProduct(temp)searchProduct(temp)替换s.searchProduct(temp)行。

Also as far as I understand from your code, in delProduct method you don't need to create a Stock object that you called s . 而且据我从您的代码了解,在delProduct方法中,您无需创建名为sStock对象。

Furthermore, you can also do this delete job by first calling a selectProduct(temp) method which will return the location of the product. 此外,您还可以通过首先调用selectProduct(temp)方法来执行此删除作业,该方法将返回产品的位置。 If the product doesn't exist, then it will return -1. 如果该产品不存在,则它将返回-1。 Then, if the result of searchProduct(temp) method is not -1, use the returned location in a deleteProduct(temp) method. 然后,如果searchProduct(temp)方法的结果不是-1,请在deleteProduct(temp)方法中使用返回的位置。 In this case, you won't bother with boolean flags to determine what to do when. 在这种情况下,您无需费心使用布尔标志来确定何时执行操作。 Just another idea. 只是另一个想法。

Good luck. 祝好运。

When you call s.searchProduct(temp), exists should become true or false based on whether it is found. 调用s.searchProduct(temp)时,根据是否找到存在,存在应该变为true或false。 Have you verified that this is happening? 您是否已验证这种情况? What is that valn variable you have in there? 您在那里的valn变量是什么?

Once that's working, you need to add a check within delete to check if exists is true and then ask for the location, otherwise to ask for more input (and then search again). 一旦完成,您需要在delete中添加一个检查以检查是否存在,然后询问位置,否则询问更多输入(然后再次搜索)。

Have you tried declaring the variable exists as volatile or maybe static so that it shall remain global for the entire class. 您是否尝试过声明变量存在为volatile或静态变量,以使其在整个类中都保持全局性。 But it will have to accessed using the classname. 但是它将必须使用类名进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM