2016-04-20 16 views
0

私は、ネストされたループメソッドをmain関数に呼び出そうとしています。私は2つのエラーcannot be resolved or is not a fieldthe method patternA() is undefined for the type classNestedを得ました。私はそれが本当に一般的なエラーだと知っていますが、私はまだそれを解決する方法を見つけることができませんでした。メイン関数の呼び出し方法

ここに私のメインクラスが

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" 
     } 

     } 

だし、ここに方法

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

     } 

    } 
} 

第一のエラーです:

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

第二のエラー:

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

誰かが間違っていることを教えてもらえますか?ありがとうございました。

+2

'.java'ファイルをどのようにコンパイルしましたか? – SomeJavaGuy

+0

コードが動作します。多分あなたは衝突の命名をしているかもしれません。 – Maroun

+1

あなたのクラス名には、最初の大文字を使用してください。 –

答えて

-4

n変数はアクセスできるように静的である必要があります。 とあなたのpatternAは、()で宣言されなければならない。この

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

いいえ、 'n 'を静的にせずに、デフォルトのパッケージ修飾子を変更する必要はありません。 – Maroun

0

のような公共の何かが私のコードは動作します...私はそれを実行しようとしていないが判明します。私はちょうど報告されたエラーで立ち往生したが、私はそれを無視して実行しようとしていない。私はそれを実行すると、エラーは魔法のように消えます。 私はeclipse btwを使用しています。

関連する問題