一個文件具有方法,另一個文件調用第一個方法。該任務是要求用戶輸入一些與他的實際引腳相對應的數字。如何製作調用其他類的方法的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已經過時了。 –