Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用&;同时比较多个值&;_Objective C_If Statement_Operators_Logic - Fatal编程技术网

Objective c 使用&;同时比较多个值&;

Objective c 使用&;同时比较多个值&;,objective-c,if-statement,operators,logic,Objective C,If Statement,Operators,Logic,我有以下代码: if ((total == (total1 && total2 && total3))) { [scrollview.contentOffset = CGPointMake (0,0)]; } 以下是按钮操作的情况: if (sender.tag == 1) { total1 = 10; } if (sender.tag == 2) { total2 =

我有以下代码:

if ((total == (total1 && total2 && total3)))
     {
        [scrollview.contentOffset = CGPointMake (0,0)];
     }
以下是按钮操作的情况:

if (sender.tag == 1)
    {
        total1 = 10;
    }

if (sender.tag == 2)
    {
        total2 = 20;
    }

if (sender.tag == 3)
    {
        total3 = 30;
    }
如果用户单击了三个正确的按钮(类似于密码键),我将尝试返回滚动视图的起始页

逻辑运算符
&&
在Objective-C中是否工作良好,我是否正确使用了它

if ((total == (total1 && total2 && total3)))
你不能那样做。您必须分别明确地比较每种方法

if ((total == total1) && (total == total2) && (total == total3)))
但这就留下了一个问题,
total
如何能同时等于这三者

你不能那样做。您必须分别明确地比较每种方法

if ((total == total1) && (total == total2) && (total == total3)))

但是这就留下了一个问题,
total
如何能够同时等于这三者。

您当前的代码本质上说的是“如果
total
为'true'且
total1
total2
total3
也都为非零,或者如果
total
为零且
total1
total2
total3
也都为零,则采取行动”

您的
&
正在进行逻辑/布尔比较。它将其参数视为
true
false
,并在任何其他情况下,如果两个参数的计算结果分别为
true
false
,则返回
true
=
总计
的值与
true
false
&
表达式中获取的值。这可能不是您想要的

似乎你想说的是“如果
total
等于
total1
total2
,和
total3
,那么做点什么”。假设是这样,你会做:

if (total == (total1 + total2 + total3)) {
    [scrollview.contentOffset = CGPointMake (0,0)];
}

您当前代码的基本意思是“如果
total
为“真”且
total1
total2
,以及
total3
也都为非零,或者如果
total
为零且
total1
total2
,以及
total3
也都为零,那么做点什么”

您的
&
正在进行逻辑/布尔比较。它将其参数视为
true
false
,并在任何其他情况下,如果两个参数的计算结果分别为
true
false
,则返回
true
=
总计
的值与
true
false
&
表达式中获取的值。这可能不是您想要的

似乎你想说的是“如果
total
等于
total1
total2
,和
total3
,那么做点什么”。假设是这样,你会做:

if (total == (total1 + total2 + total3)) {
    [scrollview.contentOffset = CGPointMake (0,0)];
}
在代码中:

if ((total == (total1 && total2 && total3)))
{
   [scrollview.contentOffset = CGPointMake (0,0)];
}
当计算if表达式时,首先计算
(total1&&total2&&total3)
。这可以是
YES
NO
(如果愿意,可以是true或false),也可以是(0或1)

因此,您的代码相当于以下代码:

BOOL allVariablesAreNotZero = total1 && total2 && total3;
if (total == allVariablesAreNotZero)
{
   [scrollview.contentOffset = CGPointMake (0,0)];
}
在问题得到更好的解释后编辑

按下按钮时,使您的按钮执行以下操作:

- (void)buttonClicked:(id)sender
{
    UIButton *button = (UIButton *)sender;
    buttonsCombination = buttonsCombination | (1 << button.tag);
}
最后,请注意,这是有效的,因为一个整数中有足够的位容纳30个按钮。 在这里,我在您的代码中使用了
|

if ((total == (total1 && total2 && total3)))
{
   [scrollview.contentOffset = CGPointMake (0,0)];
}
当计算if表达式时,首先计算
(total1&&total2&&total3)
。这可以是
YES
NO
(如果愿意,可以是true或false),也可以是(0或1)

因此,您的代码相当于以下代码:

BOOL allVariablesAreNotZero = total1 && total2 && total3;
if (total == allVariablesAreNotZero)
{
   [scrollview.contentOffset = CGPointMake (0,0)];
}
在问题得到更好的解释后编辑

按下按钮时,使您的按钮执行以下操作:

- (void)buttonClicked:(id)sender
{
    UIButton *button = (UIButton *)sender;
    buttonsCombination = buttonsCombination | (1 << button.tag);
}
最后,请注意,这是有效的,因为一个整数中有足够的位容纳30个按钮。
在这里,我使用
|
试图确定您对另外两个答案的评论是什么意思“我尝试过,但在我开始运行应用程序时它会执行代码”也许这就是您试图实现的目标:

/* all in your button handler */
switch(sender.tag)
{
   case 1:
      total1 = 10;
      break;
   case 2:
      total2 = 20;
      break;
   case 3:
      total3 = 30;
      break;
   default:
      break; // other buttons are ignored
}
// check it latest click means the total is now correct
if((total1 + total2 + total3) == total)
{
   [scrollview.contentOffset = CGPointMake (0,0)];
}

因此,您可以通过点击按钮来更新任何totalX,然后检查重置滚动的条件。

试图确定您对其他两个答案的评论是什么意思“我试过了,但在我开始运行应用程序时它会执行代码”也许这就是您试图实现的:

/* all in your button handler */
switch(sender.tag)
{
   case 1:
      total1 = 10;
      break;
   case 2:
      total2 = 20;
      break;
   case 3:
      total3 = 30;
      break;
   default:
      break; // other buttons are ignored
}
// check it latest click means the total is now correct
if((total1 + total2 + total3) == total)
{
   [scrollview.contentOffset = CGPointMake (0,0)];
}

因此,您可以通过单击按钮来更新任何totalX,然后检查重置滚动的条件。

您需要解释total、total1、total2和total3的值。好了。希望这有助于找到我的问题的答案。您有多少个按钮?当用户按下这些按钮时会发生什么,其他按钮也会发生什么?什么当用户按下其中一个按钮两次或两次以上时会出现问题?我有30个按钮,当用户得到正确的组合时,total的值将设置为零并返回起始页。其他按钮也有自己的值,当他们按下错误的组合时,会说重试。我编辑了我的答案以将其纳入account.你需要解释total、total1、total2和total3可能有哪些值。好了。希望这有助于找到我的问题的答案。你有多少个按钮?当用户按下这些按钮时会发生什么,其他按钮也会发生什么?当用户按下其中一个按钮两次或更多次时会发生什么?当用户获得co时,我有30个按钮正确组合total的值将设置回零并返回起始页。其他按钮也有自己的值,当他们按错误的组合时,它会说重试。我编辑了我的答案以考虑到这一点。我尝试了,但在我开始运行应用程序时它会执行代码。我尝试了,但它会执行代码w当我开始运行应用程序时。我尝试了它,但它在我开始运行应用程序时执行代码。我尝试了它,但它执行代码