英文:
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(" ");
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;
}
}
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论