java:如何编写类似于“如果不是(A为空)且(B为空)的条件if语句”

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

java: how to write a conditional if statement like "if not (A empty) and (B empty) then"

问题

在Java中,我需要测试在单击按钮时至少有两个单选按钮组中的一个(radiogroup1和radiogroup2)不为空(即所有单选按钮都不为空且至少有一个单选按钮被选中)。

代码结构大致如下:

  1. 按钮被点击 {
  2. 如果 (radiogroup1 不为空) 或者 (radiogroup2 不为空) {
  3. // 执行某些操作
  4. }
  5. }

当两个单选按钮组都为空时,单击按钮不会触发任何操作。

现在,我设置了类似这样的内容:

  1. if (RadioGroup.getCheckedRadioButtonId() == -1 || RadioGroup2.getCheckedRadioButtonId() == -1) {
  2. // 执行某些操作
  3. }

但实际上我需要的是完全相反的条件。我不知道如何用 != 或其他方式来反转条件。

任何帮助将不胜感激!

英文:

In Java, I need to test that at least 1 of 2 radio groups (radiogroup1 and radiogroup2) is not empty when clicking on a button (i.e., all radio buttons shall not be empty AND at least one radio button is pressed).

The code structure would be something like:

  1. button is pressed {
  2. if (radiogroup1 not empty) or (radiogroup2 not empty) {
  3. //do something
  4. }
  5. }

If both radiogroups are empty, nothing happens when the button is pressed.

Now, I set up something like this:

  1. if (RadioGroup.getCheckedRadioButtonId() == -1 || RadioGroup2.getCheckedRadioButtonId() == -1) {
  2. //do something
  3. }

but I need actually exactly the opposite. I don't succeed to invert the condition with != or something else.

Any help appreciated!

答案1

得分: 1

你是指像这样吗?

  1. RadioGroup radioGroup1; // 假设在代码的其他地方已初始化
  2. RadioGroup radioGroup2; // 假设在代码的其他地方已初始化
  3. // 检查两个单选按钮组中是否至少有一个被选中
  4. if (radioGroup1.getCheckedRadioButtonId() != -1 ||
  5. radioGroup2.getCheckedRadioButtonId() != -1) {
  6. // 进行操作
  7. }
英文:

You mean like this?

  1. RadioGroup radioGroup1; // assumed initialized someplace else in code
  2. RadioGroup radioGroup2; // assumed initialized someplace else in code
  3. // Check to see if at least one of the two radio groups has a selection
  4. if (radioGroup1.getCheckedRadioButtonId() != -1 ||
  5. radioGroup2.getCheckedRadioButtonId() != -1) {
  6. // Do stuff
  7. }

答案2

得分: 1

也许是这样的?

  1. if (!(RadioGroup.getCheckedRadioButtonId() == -1 || RadioGroup2.getCheckedRadioButtonId() == -1)) {
  2. //做一些操作
  3. }
英文:

May be this?

  1. if (!(RadioGroup.getCheckedRadioButtonId() == -1 || RadioGroup2.getCheckedRadioButtonId() == -1))
  2. {
  3. //do something
  4. }

huangapple
  • 本文由 发表于 2020年9月11日 08:41:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63839350.html
匿名

发表评论

匿名网友

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

确定