Error: cannot find symbol - method liesInt()

Error: cannot find symbol - method liesInt()

任务是"change"以下程序,以便可以创建一个子类,使用户能够通过扫描仪输入数字:

public class Patrick3 {

    static public void main(String[] emil) throws java.io.IOException {

        System.out.println("Jetzt geht es los! Geben sie eine Zahl ein");

        while (true) {
            System.out.println("Zum Beenden bitte 0 eingeben: ");
            int n = EM.liesInt();
            if (n == 0) break;

            if (n < 0) {
                System.out.println("Die Zahl " + n + " ist zu klein!");
                continue;
            }
            BigInteger erg = new BigInteger("1");
            BigInteger faktor = new BigInteger("1");
            for (int i=1; i < n; i++) {
                faktor = faktor.add(BigInteger.ONE);
                erg = erg.multiply(faktor);
            }
            String ergString = erg.toString();
            System.out.println("Die Fakultaet von " + n + " ist gleich: ");
            System.out.println(ergString);
            int laengeD = ergString.length();
            int laengeB = erg.bitLength();
            System.out.println("Länge (in Dezimalziffern) : " + laengeD);
            System.out.println("Länge (in Binaerziffern) : " + laengeB);
        } // while
        System.out.println("Das war's erstmal!");
    }
}

我这样试过:

public class Patrick_3 extends EM {
    static public int liesInt () throws IOException {

        System.out.println("Jetzt geht es los! Geben sie eine Zahl ein");

        while (true) {
            System.out.println("Zum Beenden bitte 0 eingeben: ");
            int n = EM.liesInt();
        }

public class EM
{
    public static void main (String [] args)
    {
        int i;
        boolean has_input_int;
        boolean isValid_int = false;
        String input = "";

        Scanner keyboard = new Scanner(System.in); //Decl. & int. a scanner.
        do {
            System.out.print("Geben Sie eine Int Zahl ein! ");

            while (!keyboard.hasNextInt()) {
                System.out.println("Fehler! Falsche Eingabe Versuchen sie es nochmals!");
                keyboard.next();
            }
            i = keyboard.nextInt();
            isValid_int = true;
        } while (isValid_int == false);
    }
}

但是它说

cannot find symbol - method liesint()

问题出在哪里?

因为你必须在没有这个 EM 的情况下调用 liesInt()。您永远不会创建 EM 对象,而是将其扩展到您的 class.

在您的 EM 中添加以下方法 class 或者简单地将 main 方法修改为 liesInt 如下:

public static int liesInt() {
    int i;
    boolean has_input_int;
    boolean isValid_int = false;
    String input = "";

    Scanner keyboard = new Scanner(System.in); // Decl. & int. a scanner.
    do {
        System.out.print("Geben Sie eine Int Zahl ein! ");

        while (!keyboard.hasNextInt()) {
            System.out.println("Fehler! Falsche Eingabe Versuchen sie es 
nochmals!");
            keyboard.next();
        }
        i = keyboard.nextInt();
        isValid_int = true;
    } while (isValid_int == false);
    return i;
}