gpt4 book ai didi

java - 如何在Java中的整个代码中使用一个变量的值?

转载 作者:行者123 更新时间:2023-12-01 17:47:08 24 4
gpt4 key购买 nike

使用 While 循环将文件中的行数存储在“c”变量中,但以后无法使用该变量。

我尝试使用下面的代码,但它给出了错误,例如 for 循环中的字符“c”上的符号不基金。

int i = 0;

while ((line = reader.readLine()) != null ) {
int c = ++i;
System.out.println("Count of records " + i +": " + c);
}

for (int j = 0; j < c; ++j) {
System.out.println("Element at index " + j +": " + columns[j]);
}

最佳答案

您需要在循环外声明您的 c 变量。

int i = 0, c = 0;

while ((line = reader.readLine()) != null ) {
c = ++i;
System.out.println("Count of records " + i +": " + c);
}
for (int j = 0; j < c; ++j) {
System.out.println("Element at index " + j +": " + columns[j]);
}

关于java - 如何在Java中的整个代码中使用一个变量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53946414/

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