gpt4 book ai didi

java - "cannot find symbol: variable"尽管声明了变量

转载 作者:行者123 更新时间:2023-11-29 05:03:53 25 4
gpt4 key购买 nike

当我编译下面的代码时,它说:

"error: cannot find symbol: variable max"

但我确实定义了变量max....

public class Solution {
public static boolean isOneEditDistance(String s, String t) {
if (s.length() >= t.length() ) {
StringBuilder a = new StringBuilder (s);
StringBuilder b = new StringBuilder (t);
int max = s.length();
int min = t.length();
}
else {
StringBuilder a = new StringBuilder (t);
StringBuilder b = new StringBuilder (s);
int max = t.length();
int min = s.length();
}

int flag = 0;

if ( (max-min)>1 )
return flase;
else if ( (max-min)==1 ) {
.....

最佳答案

我更喜欢如下所示的代码,这也避免了由于 a 和 b 被限定在 if 语句中包含的 block 中而导致的问题。

 if( s.length() < t.length() ){
String h = s; s = t; t = h;
}
// now s is not shorter than t
int max = s.length();
int min = t.length();
StringBuilder a = new StringBuilder(s);
StringBuilder b = new StringBuilder(t);

关于java - "cannot find symbol: variable"尽管声明了变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31080732/

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