繁体   English   中英

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

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

我不断收到以下错误:找不到符号变量查找

找不到符号方法getdata(int)

我确定我正在使这种方式变得比现在更困难,但是我不确定如何使这项工作有效,以便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  

更改

if (getdata(manID) != -1) 

进入

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

顺便说一句,这些数字不会神奇地变成二进制,因为它们仅包含0和1。 这里有一个提示:

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

编辑:响应您的下一个错误

现在将方法设为静态:

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

最终,您可能想考虑“面向对象的设计”,而仅使用main()方法作为切入点。 在main中,您可以创建一个类的实例并让其完成工作。 这样,您可以使用OO的功能(例如封装和继承),而不必使所有内容保持静态。

编辑2:事后

您的程序似乎具有以下“操作”:

  • 用户互动
  • 认证方式
  • 计算

您的域中似乎存在以下“事物”:

  • 用户
  • 密码
  • 键盘
  • 显示(命令行和屏幕)
  • 计算

OO设计的一个好的经验法则是将域中已经存在的某些“事物”和“动作”转换为类。 一个好的类只负责一个职责,并且与其他类尽可能少地共享其数据和方法(这称为信息隐藏)。

这是一个想到的类图:

  • 用户(代表用户,包含单个字段“密码”)
  • 身份验证器(对用户进行身份验证,包含允许的密码列表)
  • 控制台(所有用户交互,请使用System.out / in或Swing,但不要混合使用)
  • 计算器(计算狗屎)

暂无
暂无

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

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