2017-05-07 74 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对象,你将它扩展到你的类中。

相关问题