有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java错误:找不到符号方法liesInt()

任务是“更改”以下程序,以便可以创建一个子类,使用户可以通过扫描仪输入数字:

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()

问题在哪里


共 (2) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

    在EM类中添加以下方法,或者只需将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;
    }