2014-12-19 210 views
2

出於某種原因,我的測試程序類不能識別我的方法類中的方法。幫幫我?我得到的錯誤,如「找不到符號法digitToBarCode(INT)」和「找不到符號法校驗碼(INT)」編譯時找不到符號方法?

import java.util.Scanner; 

public class ZipCode 
{ 
public static int checkDigit(int zip) 
{ 
    int remaining = zip; 
    int sum = 0; 
    while (remaining > 0) { 
     sum += remaining % 10; 
     remaining /= 10; 
    } 
    return 10 - (sum % 10); 
} 

public static String digitToBarCode(int digit) { 
    if (digit == 1) 
    { 
     return ":::||"; 
    } 
    if (digit == 2) 
    { 
     return "::|:|"; 
    } 
    if (digit == 3) 
    { 
     return "::||:"; 
    } 
    if (digit == 4) 
    { 
     return ":|::|"; 
    } 
    if (digit == 5) 
    { 
     return ":|:|:"; 
    } 
    if (digit == 6) 
    { 
     return ":||::"; 
    } 
    if (digit == 7) 
    { 
     return "|:::|"; 
    } 
    if (digit == 8) 
    { 
     return "|::|:"; 
    } 
    if (digit == 9) 
    { 
     return "|:|::"; 
    } 
    if (digit == 0) 
    { 
     return "||:::"; 
    } 
    return ""; 

} 

} 

Tester類是這裏

import java.util.Scanner; 

public class ZipCodeConverter 
{ 
public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 
    System.out.print("Enter a zip code: "); 
    int zip = input.nextInt(); 
    int checkDigit = checkDigit(zip); 
    String digitPrint=digitToBarCode(checkDigit); 

     int specificNum1=(int)((zip/Math.pow(10, 5-1)) % 10); 
     String swag1=digitToBarCode(specificNum1); 


     int specificNum2=(int)((zip/Math.pow(10, 4-1)) % 10); 
     String swag2=digitToBarCode(specificNum2); 


     int specificNum3=(int)((zip/Math.pow(10, 3-1)) % 10); 
     String swag3=digitToBarCode(specificNum3); 


     int specificNum4=(int)((zip/Math.pow(10, 2-1)) % 10); 
     String swag4=digitToBarCode(specificNum4); 


     int specificNum5=(int)((zip/Math.pow(10, 1-1)) % 10); 
     String swag5=digitToBarCode(specificNum5); 

     String y=swag1+swag2+swag3+swag4+swag5; 


    System.out.println("|"+y+digitPrint+"|"); 
} 
} 

回答

3

當你調用的靜態方法另一個指定的類的類名

int checkDigit = ZipCode.checkDigit(zip); 
String digitPrint=ZipCode.digitToBarCode(checkDigit); 

cannot find symbol因爲編譯器無法找到ZipCodeConvertercheckDigit方法,因爲它的EXI st in ZipCode class

例如在您的代碼中,您有一條語句Math.pow(10, 3-1))Math是類名和pow()是一個靜態方法在它.so你叫它Math.pow()

0

如何調用方法checkDigit(zip);是錯誤的,checkDigit(zip)是一個靜態方法,所以它意味着它由您創建它的類所擁有。所以,

您可以通過

 int checkDigit = ZipCode.checkDigit(zip); 
    String digitPrint = ZipCode.digitToBarCode(checkDigit); 

訪問它,也可以擴展ZipCode,因爲你是這也是一個靜態調用它裏面的主:

 // this will able you to call method directly 
    public class ZipCodeConverter extends ZipCode{ 
0

您必須添加德班名稱前面的方法:

​​

必須是

int checkDigit = ZipCode.CcheckDigit(zip); 
String digitPrint=ZipCode.digitToBarCode(checkDigit);