我正在尝试学习java,并且在旧的UIL练习测试中遇到了这段代码,但我无法弄清楚这部分代码在做什么。
d[i][j] = eval(d[i-1][j]+1
[i][j-1]+1, d[i-1][j-1] + cost); /* the commas confuse me, I don't know what to add to cost */
如果有帮助的话,这是完整的代码
private static int eval(int a, int b, int c) {
int m;
m = a;
if (b < m)
m = b;
if (c < m)
m = c;
return m;
}
public static int comp(String s, String t) {
int d[][];
int n, m, i, j;
char si, tj, cost;
n = s.length();
m = t.length();
if (n == 0) {
return m;
}
if (m == 0) {
return n;
}
d = < * 1 >;
for (i = 0; i <= n; i++) {
d[i][0] = i;
}
for (j = 0; j <= m; j++) {
d[0][j] = j;
}
for (i = 1; i <= n; i++) {
si = s.charAt(i - 1);
for (j = 1; j <= m; j++) {
tj = t.charAt(j - 1);
if (si == tj) {
cost = 0;
} else {
cost = 1;
}
d[i][j] = eval(d[i - 1][j] + 1,
d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
}
}
return d[n][m];
}
最佳答案
方法中的逗号用于分隔参数。
如您所见,eval
方法需要树参数
private static int eval (int a, int b, int c){
在您的代码示例中,您像这样调用它
d[i][j] = eval(d[i - 1][j] + 1, //<-there is comma here in your code
d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
等于
d[i][j] = eval(d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1] + cost);
因此参数a
将是d[i-1][j]+1
的值,b
= d[i][j-1]+1
和c
=d[i-1][j-1] + cost
关于java - 逗号运算符在 "eval()"中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15456048/