在下面的代码中,我想在最初声明变量的 for 循环之外使用变量 a、b 和 n。例如:在 int S=a+b 中,编译器找不到 a 和 b 的位置.我怎样才能做到这一点?
{
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
}
in.close();
int S = a + b;
for (int q = 1; q < n - 1; q++) {
S = S + 2 ^ q;
System.out.print(S + " ");
}
}
}
最佳答案
简短的回答,你不能。
针对您的情况的解决方法是将这些变量的值存储在数组或映射内,这样您就可以从循环外部使用它(当然需要在循环外部声明它)
关于java - 如何在循环外使用 for 循环内使用的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59778379/