[英]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': 您的程序似乎具有以下“操作”:
And there seem to be the following 'things' in your domain: 您的域中似乎存在以下“事物”:
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: 这是一个想到的类图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.