Scanner console = new Scanner(System.in);
System.out.print("How many numbers do you have?");
int totalNumbers = console.nextInt();
double sum = 0.0;
for (int i = 1; i <= totalNumbers; i++); {
System.out.print(" #" + i + " ?");
double next = console.nextDouble();
sum += next;
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
i cannot be resolved to a variable
程序报错,说黑体那行的i有问题,但是我怎么看都找不出问题啊?
------解决方案--------------------
for (int i = 1; i <= totalNumbers; i++); 注意这个for循环后面的封号,这说明程序再这个封号前这个for循环就执行结束了,和后面的语句无关,简而言之就是这个封号造成了后面输出的i未定义的错误,把for后面的封号去掉就可以修复此错误