2016-12-03 64 views
0

一個文件具有方法,另一個文件調用第一個方法。該任務是要求用戶輸入一些與他的實際引腳相對應的數字。如何製作調用其他類的方法的java程序?

該引腳爲99508,然後從0到9的每個數字將由1,2或3的隨機數字表示。因此,用戶將輸入類似「22312」的內容而不是其實際引腳。

現在我想我已經完成了第一部分,但我不確定如何使第二個文件調用第一個方法。

這是我的第一個文件:

import java.util.Scanner; 

    public class Authenticate 
    { 

    public static void main(String[] args) 
    { 

    int[] actual_password = {9, 9, 5, 0, 8}; 

    int[] random_nums = new int[10]; 

    int[] entered_digits = new int[actual_password.length]; 

    for (int i=0; i < 10; i++) 
    { 
    random_nums[i] = (int) (Math.random() * 3) + 1; 
    } 

    System.out.println("Welcome! To log in, enter the random digits from 1-3 that"); 
    System.out.println("correspond to your PIN number."); 
    System.out.println(); 
    System.out.println("PIN digit: 0 1 2 3 4 5 6 7 8 9"); 
    System.out.print("Random #: "); 
    for (int i=0; i<10; i++) 
    { 
    System.out.print(random_nums[i] + " "); 
    } 
    System.out.println(); 
    System.out.println(); 

    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Enter code."); 
    String s = keyboard.next(); 

    int Index = 0; 
    for (int i=0; i<actual_password.length; i++) 
    { 
    String z = keyboard.next(); 
    entered_digits[Index++] = s.charAt(0) - '0'; 
    } 
    if (isValid (actual_password, entered_digits, random_nums)) 
    { 
    System.out.println("Correct! You may now proceed."); 
    } 
    else 
    { 
    System.out.println("Error, invalid password entered."); 
    } 
    } 

這裏是我的演示使用方法:

import java.util.Scanner; 

    public class AuthenticateDemo 
    { 
    public static void main(String[] args) 
    { 
      Authenticate myAuthenticate = new Authenticate(); 

    myAuthenticate.genRandomNum(); 

    System.out.println("Welcome! To log in, enter the random digits from 1-3 that"); 
    System.out.println("correspond to your PIN number."); 
    System.out.println(); 
    System.out.println("PIN digit: 0 1 2 3 4 5 6 7 8 9"); 
    System.out.print("Random #: "); 


      myAuthenticate.printRandomNum(); 
    } 
    } 

現在我得到了myAuthenticate線的錯誤,我不確定我的代碼錯了。任何幫助都會很大

+3

在'Authenticate'類中,沒有方法,所有東西都在主體中。你必須把它們放在一個方法中,因爲你沒有運行該文件,所以main已經過時了。 –

回答

0

調用另一個類的方法的一種方法是確保有問題的方法在其方法頭中有一個static關鍵字。這個例子是從一門功課我在計算機科學1班確實採取了(我在大學一年級學生,因此,如果有人有更好的答案,請回答哈哈):

一個給定的方法使用遞歸:

public class Homework8Methods{ 
public static ArrayList<int[]> permuteArray(int[] array) { 
    ArrayList<int[]> result = new ArrayList<int[]>(); 
    permute(result, array, array.length); 
    return result; 
} 

public static void permute(ArrayList<int[]> permutations, int[] a, int n) { 
    if (n == 1) { 
     permutations.add(a.clone()); 
     return; 
    } 

    for (int i = 0; i < n; i++) { 
     swap(a, i, n - 1); 
     permute(permutations, a.clone(), n - 1); 
     swap(a, i, n - 1); 
    } 
} 

public static void swap(int[] a, int i, int j) { 
    int temp = a[i]; 
    a[i] = a[j]; 
    a[j] = temp; 
} 

}

然後在一個單獨的類,它有測試用例這個特殊的問題:

System.out.println("\nPart 3:"); 
    nums = new int[]{4, 7, 1, 2}; 
    ArrayList<int[]> perms = Homework8Methods.permuteArray(nums); //Here is where the method is called from the separate class. It goes (ClassName).(MethodName with/without parameters, depending on the method in question). 
    System.out.println("permuteArray({4, 7, 1, 2}):"); 
    // uncomment the following code once you have implemented permuteArray()   
    for (int[] array: perms) { 
     for (int i = 0; i < array.length; i++) 
      System.out.print(array[i] + " "); 
     System.out.println(); 
    } 

希望這有助於!

+0

謝謝,這是對我的很好的參考。我可以理解我的代碼需要什麼。 –

+0

樂於幫忙! – BTM

0

你不能調用另一個類的不存在的方法。爲了使用在演示類的myAuthenticate.genRandomNum();方法,你需要做的方法在Authenticate類是這樣的:

public int genRandomNum(){ 
    //code to generate that random number 
    return thatNumber; 
} 

你可以說像

int randomNum = myAuthenticate.getRandomNum(); 

它看起來像訪問此你只是試圖通過將代碼放在不同的文件中來組織代碼。在面向對象編程中,你無法做得太好。 AuthenticateDemo類中的變量不能被AuthenticateDemo類訪問,除非它們是公共的,並且可以通過myAuthenticate.variableName或2返回。它們是私有的,並且您編寫一個「訪問器」方法來返回可以是通過寫myAuthenticate.getVariableName()

如果你想使用的代碼,你擁有它,現在,你可以進行身份​​驗證的main(String[] args){...}方法改寫爲類似execute(){...}和使用myAuthenticate.execute();這類失誤的對象,雖然面向的點稱之爲得到。

+0

謝謝,我開始看到我出錯的地方。現在我將嘗試編輯我的代碼以查看它是否有效。 –

相關問題