Java 主函数中的调用方法
我只是尝试将嵌套循环方法调用到main函数。我有两个错误Java 主函数中的调用方法,java,class,methods,nested-loops,Java,Class,Methods,Nested Loops,我只是尝试将嵌套循环方法调用到main函数。我有两个错误无法解决或不是字段,并且方法patternA()对于类型classNested未定义。我知道这是很常见的错误,但我还是找不到解决的办法 这是我的主要课程 package nomerTuhuh; import java.util.Scanner; public class nestedLoops { public static void main(String[] args) { // get the to
无法解决或不是字段
,并且方法patternA()对于类型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"
}
}
第二个错误:
Description Resource Path Location Type
The method patternA() is undefined for the type classNested nestedLoops.java /chapter1/src/nomerTuhuh line 16 Java Problem
谁能告诉我怎么了?谢谢。n变量应该是静态的,以便可以访问。 您的patternA()应该声明为public,如下所示
public static int n;
public void patternA()
{}
原来我的代码是有效的。。。我还没试过运行它。我只是停留在报告的错误上,但我并没有试图忽略它,只是运行它。当我运行它时,错误神奇地消失了。
顺便说一句,我正在使用eclipse。您是如何编译
.java
文件的?您的代码工作正常。可能存在命名冲突。请将类名的首字母大写。请提供运行nestedLoops
类后立即获得的确切堆栈跟踪?您是在使用eclipse、intelliJ之类的IDE,还是在命令行中运行它?编码没有问题,代码工作正常。检查其他配置否。它不需要使n
静态,也不需要更改默认的包修改器。
Description Resource Path Location Type
The method patternA() is undefined for the type classNested nestedLoops.java /chapter1/src/nomerTuhuh line 16 Java Problem
public static int n;
public void patternA()
{}