运行时扫描变量错误

Errors in scanning variables at runtime

我是 Java 编程的初学者。我想解决形式的表达 (a+20b)+(a+20b+21b)+ .........+(a+20b+...+2(n-1)b) 你在哪里given "q" queries in a, b and n for each query, 打印给定a, b, n值对应的表达式值。也就是说
示例输入:
2
0 2 10
5 3 5
示例输出:
4072
196

我的代码是:

import java.util.Scanner;

public class Expression {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);
    int q=in.nextInt();
    for(int i=0;i<q;i++){
        int a = in.nextInt();
        int b = in.nextInt();
        int 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+Math.pow(2, i)*b; //ERROR:a and b cannot be resolved to variables
    System.out.print(expr);
    in.close();
}

}

这里的错误是在 for 循环中声明了 abn,这意味着当循环结束时变量也会丢失并且垃圾收集器会处理它们。

解决这个问题真的很简单

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();
}