我不断收到以下错误: 找不到标志 变量查找
找不到符号 方法 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 中,您创建一个类的实例并让它完成其工作。通过这种方式,您可以使用 O-O 的功能,例如封装和继承,而不必使所有内容都是静态的。
编辑2:事后的想法
您的程序似乎具有以下“操作”:
- 用户互动
- 身份验证
- 计算
您的域中似乎存在以下“事物”:
- 用户
- 密码
- 键盘
- 显示(命令行和屏幕)
- 计算
O-O 设计的一个好的经验法则是将域中已经存在的一些“事物”和“操作”转换为类。一个好的类具有单一职责,并与其他类尽可能少地共享其数据和方法(这称为信息隐藏)。
这是我想到的类图:
- 用户(代表用户,包含单个字段“密码”)
- 身份 validator (对用户进行身份验证,包含允许的密码列表)
- 控制台(所有用户交互,使用 System.out/in 或 Swing,但不要混合使用它们)
- 计算器(它计算屎)
关于尝试将方法的返回值用作 if 语句中的项目时发生 Java 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3682463/