簡體   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