gpt4 book ai didi

java - sb 无法解析为变量

转载 作者:行者123 更新时间:2023-12-02 03:03:50 26 4
gpt4 key购买 nike

我尝试了这段代码,它适用于其他人,但不适合我:

//import java.lang.*;


public class Main {

public static void main(String[] args) {
String[][] states = new String[3][2];
states[0][0]="california";
states[0][1]="scremanto";
states[1][0]="oregan";
states[1][1]="salem";
states[2][0]="newyork";
states[2][1]="chicago";

for (int i = 0; i < states.length; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < states[i].length; j++) {
sb.append(states[i][j]);
}
}
System.out.println(sb);
}
}

我得到的错误是:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: sb cannot be resolved to a variable

at Main.main(Main.java:23)

注意:我使用 Eclipse,也许这很有意义。

最佳答案

您在 i for 内部定义 sb 并尝试在其外部引用它,如您所见,这是行不通的。看来您打算在循环之外定义它:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < states.length; i++) {
for (int j = 0; j < states[i].length; j++) {
sb.append(states[i][j]);
}
}

System.out.println(sb);

关于java - sb 无法解析为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42045350/

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