如何从组合框中移除列表中的项

huangapple go评论59阅读模式
英文:

how to remove from a list from a combo box

问题

以下是翻译好的部分:

当尝试编写以下代码时出现了3个错误在标记为'else'的位置有语法错误插入'assignmentoperator expression'以完成赋值并且无法将字符串转换为布尔值这是针对组合框的当我从组合框中进行选择时我希望从列表中移除其他站点。'site'是主函数中的一个变量而c.site是另一个代理程序中的参数有人可以解释应该怎么做

if (site) {
    if (c.site.equals("x")) {
        cavernIterator.remove();
        continue;
    }
} else {
    if (c.site.equals("y")) {
        cavernIterator.remove();
        continue;
    } else if (c.site.equals("z")) {
        cavernIterator.remove();
        continue;
    } else if (c.site.equals("a")) {
        cavernIterator.remove();
        continue;
    }
}
英文:

When trying to write the following code, it comes up with 3 errors. Syntax error on token 'else', insert 'assignmentoperator expression' to complete assignment and that cannot convert from string to boolean. This is for a combo box and when I choose from the combo box, I want to remove the other sites from a list. 'site' is a variable in main and c.site is a parameter in another agent. Can someone explain what to do.

if ( site ) {
	if ( c.site.equals( "x" ) ) {
		cavernIterator.remove();
		continue;
	}
}
else {
	if ( c.site.equals( "y" ) ) {
	cavernIterator.remove();
	continue;
	}
}
else {
	if ( c.site.equals( "z" ) ) {
		cavernIterator.remove();
		continue;
		}
	}
else {
	( c.site.equals( "a" ) ) {
	cavernIterator.remove();
	continue;
	}
}

答案1

得分: 2

你不能为同一个 if 包含多个 else 块。每个 else 块必须与其自己的 if 相关联:

if (a) {
    ...
} else {
    if (b) {
        ...
    } else {
        if (c) {
            ...
        } else {
            ...
        }
    }
}

或者,你可以在 else 内部使用单语句块的简写方式,代码如下:

if (a) {
    ...
} else if (b) {
    ...
} else if (c) {
    ...
} else {
    ...
}

第二种方式与第一种方式几乎相同,因为它充分利用了当你的语句块只有一条语句时,无需使用大括号来包装语句块,示例如下:

if (myCondition)
    System.out.println("My condition passed");
else
    System.out.println("My condition did not pass");
英文:

You can't have several else blocks for the same if. Each else block has to be attached to its own if:

if(a) {
    ...
} else {
    if (b) {
        ...
    }
    else {
        if (c) {
            ...
        }
        else {
            ...
        }
    }
}

Or, with a shorthand for all this, you can have a single-statement block inside the else, so it looks like this:

if (a) {
    ...
}
else if (b) {
    ...
}
else if (c) {
    ...
}
else {
    ...
}

This second one is nearly the same as the first, as it takes advantage of not having to wrap a statement block in braces if your statement block only has single statement, like this:

if (myCondition)
    System.out.println("My condition passed");
else
    System.out.println("My condition did not pass");

答案2

得分: 1

另外如果应该这样写
```java
if (...) {
    // 此处为代码
} else if (...) {
    // 此处为代码
} else {
    // 此处为代码
}

此外,你的情况似乎相似

String[] sites = {"x", "y", "z", "a"};
boolean contains = Arrays.stream(sites).anyMatch(c.site::equals);
if (contains) {
    cavernIterator.remove();
    continue;
}

编辑:考虑到site是一个字符串

String site = "x"; // 提供默认值
String[] sites = {"x", "y", "z", "a"};
boolean condition = site.equals(c.site) && Arrays.stream(sites).anyMatch(c.site::equals);
if (condition) {
    cavernIterator.remove();
    continue;
}
英文:

Else if should be written this way :

if (...) {
    // Code here
} else if (...) {
    // Code here
} else {
    // Code here
}

Also your cases seem similar

String[] sites = {"x","y","z","a"};
boolean contains = Arrays.stream(sites).anyMatch(c.site::equals);
if (contains) {
    cavernIterator.remove();
    continue;
}

EDIT: Considering site is a string

String site = "x"; // Default value provided
String[] sites = {"x","y","z","a"};
boolean condition = site.equals(c.site) && Arrays.stream(sites).anyMatch(c.site::equals);
if (condition) {
    cavernIterator.remove();
    continue;
}

答案3

得分: 0

if (site) {
    if (c.site.equals("x")) {
    }
}

equals 

if (site && c.site.equals("x")) {
}

And your last else is messed up.

Likely look at some Java tutorials because you're writing the least efficient way possible. This is your code in short:

if (site && (c.site.equals("x") || c.site.equals("y") || c.site.equals("z") || c.site.equals("a"))) {
    cavernIterator.remove();
}
英文:
    if ( site ) {
        if ( c.site.equals( "x" ) ) {
        }
   }

equals

    if(site && c.site.equals("x")){
    }

And your last else is messed up.

Likely look on some Java tutorials because you write the least efficient way possible. This is your code in short:

if (site && (c.site.equals("x") || c.site.equals("y") || c.site.equals("z") || c.site.equals("a"))) {
	cavernIterator.remove();
}

huangapple
  • 本文由 发表于 2020年9月3日 16:52:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63720131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定