2016-04-20 10 views
0

Ich versuche nur, verschachtelte Schleifen Methode Hauptfunktion aufzurufen. Ich habe 2 Fehler cannot be resolved or is not a field und the method patternA() is undefined for the type classNested. Ich weiß, dass es wirklich ein häufiger Fehler ist, aber ich konnte immer noch keinen Weg finden, es zu lösen.Aufruf Methode in Hauptfunktion

Hier ist meine Hauptklasse

package nomerTuhuh; 

import java.util.Scanner; 

public class nestedLoops { 

    public static void main(String[] args) { 
      // get the total number of lines n. 

     classNested result = new classNested(); 
     Scanner sc = new Scanner(System.in); 

      System.out.print("Enter the number of lines:"); 
      result.n=sc.nextInt(); <-- error "cannot be resolved or is not a field" 

     result.patternA(); <-- error "the method patternA() is undefined for the type classNested" 
     } 

     } 

Und hier ist die Methode

package nomerTuhuh; 

import java.util.Scanner; 

public class classNested { 
     public int n; 


     void patternA(){ 

     // Loop through the lines from 1 to n 
     System.out.println("Pattern A"); 
     for (int i = 1; i <= n; i++) { 

     // Printing number increamentally from 1 to line number j 
     for (int j = 1; j <= i; j++) { 
     System.out.print(j + " "); 
     } 
     System.out.println(); 

     } 

    } 
} 

1. Fehler:

Description Resource Path Location Type 
n cannot be resolved or is not a field nestedLoops.java /chapter1/src/nomerTuhuh line 14 Java Problem 

2. Fehler:

Description Resource Path Location Type 
The method patternA() is undefined for the type classNested nestedLoops.java /chapter1/src/nomerTuhuh line 16 Java Problem 

Kann mir bitte jemand sagen, was los ist? Vielen Dank.

+2

Wie haben Sie Ihre '.java' Dateien kompiliert? – SomeJavaGuy

+0

Ihr Code funktioniert. Vielleicht haben Sie Namenskollision. – Maroun

+1

Können Sie bitte das Anfangsbuchstabe Kapital für Ihre Klassennamen bilden. –

Antwort

-4

Die Variable n sollte statisch sein, damit auf sie zugegriffen werden kann. und Ihre patternA() sollte meinen Code öffentlich so etwas wie dieses

public static int n; 
public void patternA() 
{} 
+0

Nein. Es funktioniert, ohne "n" statisch zu machen und ohne den Standard-Modifikator für Pakete ändern zu müssen. – Maroun

0

Es stellte sich heraus funktioniert ... Ich habe es zu laufen nicht versucht, zu erklären. Ich steckte einfach auf den gemeldeten Fehler, aber ich versuche nicht, es zu ignorieren und es einfach auszuführen. Wenn ich es starte, verschwindet der Fehler auf magische Weise. Ich benutze Eclipse BTW.