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

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

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

问题

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

代码结构大致如下:

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

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

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

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

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

任何帮助将不胜感激!

英文:

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:

button is pressed {
if (radiogroup1 not empty) or (radiogroup2 not empty) {
//do something
}
}

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

Now, I set up something like this:

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

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

Any help appreciated!

答案1

得分: 1

你是指像这样吗?

RadioGroup radioGroup1; // 假设在代码的其他地方已初始化
RadioGroup radioGroup2; // 假设在代码的其他地方已初始化

// 检查两个单选按钮组中是否至少有一个被选中
if (radioGroup1.getCheckedRadioButtonId() != -1 || 
    radioGroup2.getCheckedRadioButtonId() != -1) {
    // 进行操作
}
英文:

You mean like this?

RadioGroup radioGroup1; // assumed initialized someplace else in code
RadioGroup radioGroup2; // assumed initialized someplace else in code

// Check to see if at least one of the two radio groups has a selection
if (radioGroup1.getCheckedRadioButtonId() != -1 || 
    radioGroup2.getCheckedRadioButtonId() != -1) {
	// Do stuff
}

答案2

得分: 1

也许是这样的?

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

May be this?

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

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:

确定