嘿,抱歉,这是一个很长的问题,但我无法使用 pm.printPASSInfo() 将 printPASSInfo() 调用到另一个类。 pm 是我为我尝试调用的方法所在的类命名的名称。我可以很好地调用方法 pm.printSSNInfo 我不明白出了什么问题。很抱歉,这可能令人困惑且很长,但请尽力提供帮助。多谢你们!这是我的代码:
import java.util.Scanner;
public class Prog1Methods_FA11 {
String ssn, pw, phoneNumber,line;
Scanner input = new Scanner (System.in);
boolean validPW_Length = true,
validPW_Symbols = true,
validPW_enough_Digits = true;
boolean validSSN_Digits = true,
validSSN_Format = true,
validSSN_Length = true;
boolean validPhone_Symbols = true,
validPhone_Format = true,
validPhone_Length = true;
public Prog1Methods_FA11() {
}
// you may insert a method here to display password status
public void printPASSInfo(){
System.out.println("\t Password Information");
System.out.println("The Password:\t" + pw);
System.out.println("Password Lrngth:\t" + validPW_Length);
System.out.println("Password has minimum number of digits:\t" + validPW_enough_Digits);
System.out.println("Password has correct symbols:\t" + validPW_Symbols);
}
// you may insert a method here to display the phone number status
}
这是我试图调用它的地方:
case 2: System.out.println("Enter a password witha atleast 8 characters and atleast 2 numbers:\t");
pw = input.nextLine();
pm.readAndVerifyPASS(pw);
pm.printPASSInfo();
break;
和编译错误:
MySkeletonProgram1_FA11.java:53: cannot find symbol
symbol : method printPASSInfo()
location: class Prog1Methods_FA11
pm.printPASSInfo();
^
1 error
我在哪里声明 pm 对象:
public class MySkeletonProgram1_FA11{
public static void main(String[] args)throws Exception {
// Declarations
Scanner scan = new Scanner(System.in);
Scanner input = new Scanner (System.in);
Prog1Methods_FA11 pm = new Prog1Methods_FA11();
最佳答案
我不确定我对你的问题的理解程度如何。
我只是尝试重现该错误。
所以,我做了什么。
1.文件MySkeletonProgram1_FA11.java
public class MySkeletonProgram1_FA11 {
public static void main(String[] args) throws Exception {
Prog1Methods_FA11 pm = new Prog1Methods_FA11();
pm.printPASSInfo();
}
}
2.文件Prog1Methods_FA11.java
import java.util.Scanner;
public class Prog1Methods_FA11 {
String ssn, pw, phoneNumber, line;
Scanner input = new Scanner(System.in);
boolean validPW_Length = true,
validPW_Symbols = true,
validPW_enough_Digits = true;
boolean validSSN_Digits = true,
validSSN_Format = true,
validSSN_Length = true;
boolean validPhone_Symbols = true,
validPhone_Format = true,
validPhone_Length = true;
public Prog1Methods_FA11() {
}
// you may insert a method here to display password status
public void printPASSInfo() {
System.out.println("\t Password Information");
System.out.println("The Password:\t" + pw);
System.out.println("Password Lrngth:\t" + validPW_Length);
System.out.println("Password has minimum number of digits:\t" + validPW_enough_Digits);
System.out.println("Password has correct symbols:\t" + validPW_Symbols);
}
}
3.我已将这两个文件放在同一目录中。
4.编译命令
javac MySkeletonProgram1_FA11.java
编译成功完成,没有错误和警告。
这对你有用吗?如果答案是“否”,那么我认为这是你的 JDK 的问题。否则您可能需要提供更多详细信息。你做了什么不同的事情?
关于Java 代码不允许我从一个类调用另一个类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7169930/