运行时扫描变量的错误

时间:2018-06-17 11:22:52

标签: compiler-errors expression java.util.scanner unresolved-external

我是Java编程的初学者。我想解决表单的表达式 (A + 2 0 B)+(A + 2 0 B + 2 1 B)+ ........ + (a + 2 0 b + ... + 2 (n-1) b)你被给予" q"对于每个查询,以a,b和n的形式查询,打印对应于给定的a,b和n值的表达式值。这意味着 样本输入:
2
0 2 10
5 3 5
样本输出:
4072
196

我的代码是:

class StockData_Admin(admin.ModelAdmin):
 list_display = (
    'get_date',
    'get_stock',
    'get_interval'
 )

 def get_date(self, obj):
     return obj.date

 def get_stock(self, obj):
     return obj.stock

 def get_interval(self, obj):
     return obj.interval

 get_date.empty_value_display = 'No available date'
 get_stock.empty_value_display = 'No available stock'
 get_interval.empty_value_display = 'No available interval'

 admin.site.register(StockData,StockData_Admin)

1 个答案:

答案 0 :(得分:1)

此处的错误是在a循环中声明bnfor,这意味着当循环结束时变量也将丢失并且垃圾收藏家会照顾他们。

解决方案非常简单

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);
    int q=in.nextInt();
    int a, b, n;               // Declare outside if you need them outside ;)
    for(int i=0;i<q;i++){
        a = in.nextInt();
        b = in.nextInt();
        n = in.nextInt();
    }
    int expr=a+b;              //ERROR:a cannot be resolved to a variable
    for(int i = 0; i<n;i++) {  //ERROR:n cannot be resolved to a variable
        expr+=a+(2*i)*b;       //ERROR:a and b cannot be resolved to variables
        System.out.print(expr);
    }
    in.close();
}