一个文件具有方法,另一个文件调用第一个方法。该任务是要求用户输入一些与他的实际引脚相对应的数字。如何制作调用其他类的方法的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
线的错误,我不确定我的代码错了。任何帮助都会很大
在'Authenticate'类中,没有方法,所有东西都在主体中。你必须把它们放在一个方法中,因为你没有运行该文件,所以main已经过时了。 –