主函数中的调用方法

时间:2016-04-20 07:37:35

标签: java class methods nested-loops

我只是试图将嵌套循环方法调用到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

任何人都可以告诉我错误的是什么?谢谢。

1 个答案:

答案 0 :(得分:0)

原来我的代码有效......我还没试过去运行它。我只是坚持报告错误,但我不是试图忽略它而只是运行它。当我运行它时,错误神奇地消失了。 我正在使用eclipse btw。