gpt4 book ai didi

java - 错误: cannot find symbol symbol: variable mes location: class ventasArreglo

转载 作者:行者123 更新时间:2023-11-30 07:54:23 26 4
gpt4 key购买 nike

请帮忙,我是java新手。我必须使用数组、for 和子例程来做作业。这是我到目前为止的代码:

import java.util.*;
import static java.lang.System.out;
public class ventasArreglo{
static int dias, semanas, i, j;
static Scanner kb=new Scanner(System.in);

public static void main(String args[]){
dias=5;
semanas=4;
int mes[][] = new int[semanas][dias];
introducirDatos();

}

static void introducirDatos(){
for(i=0;i<semanas;i++){
for(j=0;j<dias;j++){
out.println("Cantidad de Ventas");
mes[i][j]=kb.nextInt();
}
}
}
}

但编译后我不断收到此错误: 发现 1 个错误:[行:20]

Error: cannot find symbol
symbol: variable mes
location: class ventasArreglo

最佳答案

变量 mes 的声明应移至外部,以便静态方法 introducirDatos 可见:

    static int dias, semanas;
static int[][] mes;

public static void main(String[] args) {
dias=5;
semanas=4;
mes = new int[semanas][dias];
introducirDatos();

}
static void introducirDatos(){
Scanner kb=new Scanner(System.in);
for(int i=0;i<semanas;i++){
for(int j=0;j<dias;j++){
out.println("Cantidad de Ventas");
mes[i][j]=kb.nextInt();
}
}
}

此外,Scanner 应该移动到实际需要的方法内部,而不是在顶层将其声明为静态。循环计数器也不必位于顶层。

但是为什么要使用静态导入呢?我们应该谨慎使用它们。正如 doc 中提到的:

So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). ... If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import.

检查这个link也是如此。

关于java - 错误: cannot find symbol symbol: variable mes location: class ventasArreglo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32877473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com