满足值时,while循环不会退出 [英] while loop not exiting when value met

查看:162
本文介绍了满足值时,while循环不会退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况要检查应用程序中单元格的值。当单元格的值达到0或-1时,我希望测试继续进行。所以我有:

I have a scenario where I want to check the value of a cell in an application. When the value of the cell hits 0 or -1 I'd like the test to continue. So I have:

while (!cell.Value.Equals("0") || !cell.Value.Equals("-1")) 
{
    Console.WriteLine("Value ({1})",cell_value.Value);
    Thread.Sleep(15000);
}

不幸的是,当单元格达到0时,它似乎不会中断 ' 跳出循环。

Unfortunately, when the cell reaches 0, it doesn't appear to 'break' out of the loop.

Output:
    Value (20)
    Value (13)
    Value (10)
    Value (9)
    Value (4)
    Value (1)
    Value (0)
    Value (0)
    Value (0)
    Value (0)

最好使用while / do + while循环或

Is it best to try this with a while / do + while loop or is a for loop better?

谢谢,

J。

推荐答案

|| 表示至少一个必须为true。因此,当您输入0时,您得到的是:

|| means that at least one must be true. Therefore when you enter 0 what you get is:


  1. !cell.Value.Equals( 0)-不正确 = false

  2. !cell.Value.Equals( -1)-不是false = true

  1. !cell.Value.Equals("0") - "not true" = false
  2. !cell.Value.Equals("-1") - "not false" = true

,因此它进入。您想改用&

and therefore it enters. You want to be using && instead:

while (!cell.Value.Equals("0") && !cell.Value.Equals("-1")) 

或这样写:

// while not (equals 0 or -1)
while (!( cell.Value.Equals("0") || cell.Value.Equals("-1")))

这篇关于满足值时,while循环不会退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆