2017-05-07 35 views
1

的任務是「變」下面的程序,以便它可以創建一個子類,可以使用戶通過掃描儀輸入數字:錯誤:無法找到符號 - 方法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()

問題出在哪裏?

+1

你能下來減少這種代碼,以再現錯誤的絕對最低?見[mcve] –

+0

對不起。當然! – Phoney

+0

什麼是EM?錯誤消息涉及的代碼行是什麼? –

回答

0

添加以下在EM類方法或者簡單修改的​​主要方法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; 
} 
+0

非常感謝!有用! – Phoney

0

因爲您必須在沒有此EM的情況下調用liesInt()。你永遠不會創建一個EM對象,你將它擴展到你的類中。