2016-12-03 147 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

谢谢,我开始看到我出错的地方。现在我将尝试编辑我的代码以查看它是否有效。 –

相关问题