简体   繁体   English

Java错误尝试将方法的返回值用作if语句中的项时

[英]Java Error When attempting to use the return from a method as an item in an if statement

I keep getting the following errors: Cannot find symbol Variable find 我不断收到以下错误:找不到符号变量查找

cannot find symbol method getdata(int) 找不到符号方法getdata(int)

I am sure I am making this way more difficult than it is, but I am not sure how make this work so that the return from searching through the array, can be seen and evaluated by the if statement. 我确定我正在使这种方式变得比现在更困难,但是我不确定如何使这项工作有效,以便if语句可以看到并评估通过数组搜索得到的返回值。

                   //assigns manager identification 
                    manID = keyboard.nextInt();

        //Fibonacci binary array for passwords
        int[] passWArray = {00000000,00000001,00000001,00000010,00000011,00000101,00001000,00001101};

        //item = find.getdata(manID);

        if (getdata(manID) != -1) 
        {
        //Do work here
        dblPayRate = 10.85;
        dblGrossPay =  (intHours * dblPayRate) + (15.00);
        dblTaxes = dblGrossPay * 0.19;
        dblGrossPay -= dblTaxes;

        //Print information to user
        System.out.print("\n\n$" + df2.format(dblTaxes) + 
        " was withheld from this paycheck in taxes after working "+ intHours + " hours.\n\n");
        System.out.print("The amount \"Employer Here\" owes you is $" + df2.format(dblGrossPay) + "\n");
        }
        else
        {
        // Dialog box for incorrect password
        JOptionPane.showMessageDialog(null, "Invalid Entry! Contact the BOFH!");
        //exits program (Note: needed for any JOptionPane programs)
        System.exit(0); 
        }
    }// end of long if statement for >50 hours
}//end of main method

 public int find(int[] passWArray, int manID) 
{
    //search for manID in passWArray array
    for (int index = 0; index < passWArray.length; index++) 

        if ( passWArray[index] == manID )

            return manID;
    //-1 indicates the value was not found      
    return -1;

}// end of find method  

Change 更改

if (getdata(manID) != -1) 

into 进入

if (find(passWArray , manID) != -1) 

BTW those numbers don't magically become binary because they only contain 0's and 1's. 顺便说一句,这些数字不会神奇地变成二进制,因为它们仅包含0和1。 Here's a hint: 这里有一个提示:

int thirteen = Integer.parseInt("00001101", 2)

EDIT: in response to your next error 编辑:响应您的下一个错误

For now make the method static: 现在将方法设为静态:

public static int find(int[] passWArray, int manID) 

Eventually you might want to think about your 'Object-Oriented design' and just use the main() method as an entry point. 最终,您可能想考虑“面向对象的设计”,而仅使用main()方法作为切入点。 Within main you create an instance of a class and let it do its work. 在main中,您可以创建一个类的实例并让其完成工作。 In this way you can use the powers of OO like encapsulation and inheritance and don't have to make everything static. 这样,您可以使用OO的功能(例如封装和继承),而不必使所有内容保持静态。

EDIT2: Afterthought 编辑2:事后

Your program seems to have the following 'actions': 您的程序似乎具有以下“操作”:

  • user interaction 用户互动
  • authentication 认证方式
  • calculation 计算

And there seem to be the following 'things' in your domain: 您的域中似乎存在以下“事物”:

  • user 用户
  • password 密码
  • keyboard 键盘
  • display (command line and screen) 显示(命令行和屏幕)
  • calculation 计算

A good rule of thumb for an OO design is to convert some of the 'things' and 'actions' already present in your domain into classes. OO设计的一个好的经验法则是将域中已经存在的某些“事物”和“动作”转换为类。 A good class has a single responsibility and shares as little as possible of its data and methods with other classes (this is called information hiding). 一个好的类只负责一个职责,并且与其他类尽可能少地共享其数据和方法(这称为信息隐藏)。

Here's a class diagram that comes to mind: 这是一个想到的类图:

  • User (represents a user, contains a single field 'password') 用户(代表用户,包含单个字段“密码”)
  • Authenticator (authenticates a user, contains the list of allowed passwords) 身份验证器(对用户进行身份验证,包含允许的密码列表)
  • Console (all user interaction, either use System.out/in or Swing, but don't mix them) 控制台(所有用户交互,请使用System.out / in或Swing,但不要混合使用)
  • Calculator (it calculates shit) 计算器(计算狗屎)

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

相关问题 尝试从 Java 中的方法返回 float[] - Attempting to return a float[] from a method in Java 尝试在Java中使用自定义比较器时出错 - Error when attempting to use custom comparator in Java 尝试从 while 循环返回时出现无法访问的语句错误 - Java - Unreachable statement error when trying to return from a while loop - Java 方法错误 - Java 中缺少 return 语句 - Method error - missing return statement in Java 尝试使用.contains()Java方法 - Attempting to use .contains() Java method Java / Android Studio-协方差返回类型-“子类”中的方法与“基类”中的方法冲突:尝试使用不兼容的返回类型 - Java/Android Studio - Covariant Return Type - Method in “subclass” clashes with method in “baseclass”: attempting to use incompatible return type 尝试在Java中使用List &lt;&gt;时出现“非通用”错误 - Getting “not generic” error when attempting to use List<> In Java 尝试从文档中检索Java中的节点值时出错 - Error when attempting to retrieve node value in Java from Document 尝试在JavaScript中的Java变量上使用.get方法 - Attempting to use .get method on java variable in javascript 在方法本身调用后有return语句的情况下,以Java递归 - Recursive in Java when there is a return statement after the method called itself
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM