繁体   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); 
        }
    }
}

实际上,一切都很好。 但事实是,如果用户输入的product name不存在于矢量中,则仍然希望他输入要删除的位置。 (显然,它正在执行应该执行的操作)。 我尝试过声明如果找到搜索项就将其设置为true的其他布尔变量,并且仅在布尔值为true时才执行删除过程,但是在delete方法中,该变量始终为false。 有什么想法吗? 顺便说一句,如果我犯了愚蠢的错误,对不起,我还是Java的新手。

问题是,当您执行s.searchProduct(temp) exists ,如果找到了产品,您期望它exists ,但事实并非如此。 因为在执行s.searchProduct(temp) ,您是从另一个名为s Stock对象中调用方法searchProduct(temp) 因此,如果乘积以temp exists ,则s exists将为真,因此s.exists将为真。 但是,您想要真实的是this.exists 为此,您必须用this.searchProduct(temp)searchProduct(temp)替换s.searchProduct(temp)行。

而且据我从您的代码了解,在delProduct方法中,您无需创建名为sStock对象。

此外,您还可以通过首先调用selectProduct(temp)方法来执行此删除作业,该方法将返回产品的位置。 如果该产品不存在,则它将返回-1。 然后,如果searchProduct(temp)方法的结果不是-1,请在deleteProduct(temp)方法中使用返回的位置。 在这种情况下,您无需费心使用布尔标志来确定何时执行操作。 只是另一个想法。

祝好运。

调用s.searchProduct(temp)时,根据是否找到存在,存在应该变为true或false。 您是否已验证这种情况? 您在那里的valn变量是什么?

一旦完成,您需要在delete中添加一个检查以检查是否存在,然后询问位置,否则询问更多输入(然后再次搜索)。

您是否尝试过声明变量存在为volatile或静态变量,以使其在整个类中都保持全局性。 但是它将必须使用类名进行访问。

暂无
暂无

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

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