我想以这样的方式给出一个条件:如果所有值都是0或1,则执行一些逻辑.
我有三个变量.
0 0 0 -> evaluates to True
1 1 1 -> evaluates to True
Run Code Online (Sandbox Code Playgroud)
所有其他组合评估为False.我该如何为此编写逻辑?
以下是解释我的问题的代码示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
try
{
Program.Sample();
Console.ReadLine();
}
catch (Exception)
{
Console.WriteLine("InvalidOperation Exception");
}
}
static bool Sample()
{
int a = 1; int b = 2;
if (a == b) //Some Condition checking
{
return true;
}
else
{ //Is this Proper returning Exception In Boolean method instead of False
throw new InvalidOperationException();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在客户端应用程序中捕获此异常并显示消息,我想知道这是否是正确的方法?
我试图理解以下代码:
for f in *.out; do sort -cg <$f &>/dev/null && res="sorted" || res="unsorted"; echo "File $f is $res."; done
Run Code Online (Sandbox Code Playgroud)
For 循环遍历所有.out
文件并将每个文件作为参数给出sort
,排序的输出被重定向为“nothing”。但有人能解释一下&& res="sorted" || res="unsorted"
吗?
我正在尝试用汇编语言编写 XOR 运算,但我们允许使用的唯一运算是 AND 和 NOT,不是 OR,也绝对不是 XOR。我在网上到处查了一下,似乎找不到答案。我知道: XOR = (P 或 Q)和 ~(P 和 Q) 但我需要用 AND 运算重写(P 或 Q) 。这可能吗?
assembly boolean-logic boolean-expression demorgans-law boolean-algebra
今天我在这个Python程序中遇到了一个困难的情况:
a = [False, True]
x = True in a in a
y = True in a in [a]
z = True in a in (a)
print(x, y, z)
Run Code Online (Sandbox Code Playgroud)
这段代码的输出是
a = [False, True]
x = True in a in a
y = True in a in [a]
z = True in a in (a)
print(x, y, z)
Run Code Online (Sandbox Code Playgroud)
这怎么可能?
让我们在x
这里测试:
x = True in a in a
Run Code Online (Sandbox Code Playgroud)
True in [False, True]
是True
,又True in [False, True] …
让我们看看这个简单的Java代码:
class A {
public static void main(String[] args) {
if (1 == 1)
if (2 == 2)
if (2 != 2) // <-- should end here and do nothing
System.out.println("2 != 2");
else
System.out.println("2 != 2");
else
System.out.println("1 != 1");
}
}
Run Code Online (Sandbox Code Playgroud)
正如评论所说,它应该看到1==1
,然后2==2
,最嵌套的条件2!=2
失败,所以程序退出而不打印任何东西.但事实并非如此,相反它说2!=2
:
$ javac A.java && java A
2 != 2
Run Code Online (Sandbox Code Playgroud)
为什么?
奇怪的是,它在Python中按预期工作:
>>> if (1 == 1):
... if (2 == 2):
... if (2 != 2):
... print("2 …
Run Code Online (Sandbox Code Playgroud) 我试图看看我可以用for循环做些什么很酷的事情.这是一个简单的代码,用于打印在10_000和100_000之间可被321整除的所有数字.
但它不起作用:
for(int i=10000;i<=100000 && i%321==0;i++){
println(i);
}
Run Code Online (Sandbox Code Playgroud)
我可以在第二个和第三个分号之间加上条件语句,这不是真的吗?这只是输出什么,所以我假设没有我的值是真的.
Haskell初学者在这里.我想证明NAND在haskell中分布在NOR上.这应该让我回到"真实",但我一直都是假的.我甚至不确定我是否正确接近它,但这是我尝试过的:
nand_distributes_nor :: Bool
nand_distributes_nor = and [(a `nand` (b `nor` c)) == ((a `nand` b) `nor` (a `nand` c)) | a<-bools, b<-bools, c<-bools]
Run Code Online (Sandbox Code Playgroud) 我收到错误:
错误:(65,3)java:非法启动表达式
参考这一行:
public boolean equals(WordList wordList)
Run Code Online (Sandbox Code Playgroud)
我认为这是由字符串数组的范围引起的WordList[]
.但是,这似乎应该是可以接受的,因为我在构造函数中调用变量的实例.
我曾试图改变WordList[]
建设public equals(WordList wordList)
,boolean equals(WordList wordList)
以及其他的组合,虽然这些组合已经改变了错误信息.
码:
public class WordList
{
String[] words;
public int count;
//constructor
public WordList()
{
//create a size two array of strings and assign it to words instance variable
words = new String[2];
count = 0;
}
public int addWord(String word)
{
if(findWord(word) == -1) //word not in list
{
return count;
}
if(words.length == count)
{
String[] temp = …
Run Code Online (Sandbox Code Playgroud) 我即将在Prolog中实现逻辑术语的证明者.我当前的代码并不是真的可以呈现,因此,我只是说,我想要我的程序做什么,希望你可以给我一些很好的建议:)
它应该采用变量列表(也就是说逻辑参数),其次是包含这些参数的逻辑公式(例如'not'(A 'and' B) 'or' 'not'(B 'and' C) 'or' ...
,依此类推).
作为输出,我希望我的程序能够以可能的一致性分配进行响应.单个参数可以是true(1
)或false(0
).
所以我的目标是回归A=0, B=0, C=0 ; A=1
等等.
我很高兴有关我的计划的每一个帮助:)