收到“找不到符号”错误,尽管所有内容都在范围内声明。

huangapple go评论64阅读模式
英文:

Receiving Cannot find symbol error, even though everything is declared within the scope

问题

抱歉,如果这个问题有一个简单的解决方法,我刚开始学习Java。我正在尝试编译这一段代码,但是一直收到一个错误,指出我的for循环语句中有一个"Cannot find Symbol",并指向'imp.length()'。

public static void main(String[] args) {
    double[] imp = new double[7];
    Scanner lengths = new Scanner(System.in);
    String input = lengths.nextLine();
    String[] inp = input.split(" ");

    try{
        for(int i = 0; i < imp.length; i++){
            double len = Double.parseDouble(inp[i]);
            imp[i] = len;
            if(imp[i] < 0){
                System.out.println("Invalid Input.");
                break;
            }
        }
    }

我已经多次重新编写了这段代码,以确保所有内容都在正确的作用域内,但是我仍然得到相同的错误。

英文:

Preface: Sorry if this question has an easy fix, I am fairly new to learning java.

I am trying to compile this block of code, but keep receiving an error stating "Cannot find Symbol" for my for statement, pointing towards 'imp.length().

	public static void main(String[] args) {
	double[] imp = new double[7];
	Scanner lengths = new Scanner(System.in);
	String input = lengths.nextLine();
	String[] inp = input.split(&quot; &quot;);

	try{
		for(int i = 0; i &lt; imp.length(); i++){
			double len = Double.parseDouble(inp[i]);
			imp[i] = len;
			if(imp[i] &lt; 0){
				System.out.println(&quot;Invalid Input.&quot;);
				break;
			}
		}
	}

I have rewritten the block of code multiple times to ensure that everything is within the correct scope, but I continue to get the same error.

答案1

得分: 0

错误是因为数组没有.length()方法,但是数组有一个.length属性(没有括号)。

英文:

The error is because there is no .length() method of an array, but there is a .length attribute of the array. (No parentheses).

答案2

得分: 0

只使用长度,不使用length() ...

for(int i = 0; i < imp.length; i++)

而且,如果你在使用try,必须使用catch。

英文:

Jut use length, don't use length() ...

for(int i = 0; i < imp.length; i++)

and, if you are using try you must use catch

huangapple
  • 本文由 发表于 2020年9月6日 11:03:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63760337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定