[英]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設計的一個好的經驗法則是將域中已經存在的某些“事物”和“動作”轉換為類。 一個好的類只負責一個職責,並且與其他類盡可能少地共享其數據和方法(這稱為信息隱藏)。
這是一個想到的類圖:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.