标签: boolean

C#,如果另一个方法返回 true 则启动方法

我有以下代码:

static void Start()
        {
            First();
            Second();
            ...
        }


    private bool First()
        {
           if()
                 {.....
                  return true;
                 }
          return false;
        }
Run Code Online (Sandbox Code Playgroud)

可能有一个简单的解决方案,但我只想Second();在该First();方法返回 true时启动该方法

非常感谢你的帮助。

c# boolean

-3
推荐指数
1
解决办法
338
查看次数

-3
推荐指数
1
解决办法
2439
查看次数

如何将布尔值从 true 更改为 false,反之亦然

我在 Go 中需要改变 bool 值并存储它。我收到 value == true 但我需要更改它并存储更改后的值。我可以想到只将警报存储到 var 并在下一个语句中传递它。

例如伪代码:

chnagevalue := false

if value == false {
     changevalue == true 
}
Run Code Online (Sandbox Code Playgroud)

在 Go 中最好的方法是什么?有没有预先定义的方法来做到这一点?

boolean-logic boolean go

-3
推荐指数
1
解决办法
1013
查看次数

将布尔对象与布尔基元进行比较

这段代码的输出是什么?请解释如何在此处完成Autoboxing或拆箱ID.

class MyBoolean
{
    Boolean[] bool = new Boolean[5];

    public static void main(String[] args)
    {
      new MyBoolean().myMethod();
    }

    public void myMethod()
    {
      if(bool[1]==true)
      {
        System.out.println("It's true");
      }
      else
      {
        System.out.println("It's false");
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

java boolean

-4
推荐指数
1
解决办法
3154
查看次数

布尔的大小是多少?1位还是1个字节?

printf("bool: %zu; true: %zu; 1: %zu\n", sizeof (bool), sizeof true, sizeof 1);
Run Code Online (Sandbox Code Playgroud)

上面的代码产生了

bool: 1; true: 1; 1: 4
Run Code Online (Sandbox Code Playgroud)

我对sizeof(1)很好,它是一个整数,因此大小为4字节.但bool (and true)只需要一个大小1 "bit",输出仍然是1即1个字节.

为什么会这样?

c c++ gcc boolean

-4
推荐指数
1
解决办法
802
查看次数

c ++ bool返回true

我正在制作一个tic-tac-toe控制台应用程序,我主要完成它.还有一些我要添加的东西,但我遇到了一个大问题.我有一个bool checkwin()函数,它应该看看游戏是否已经赢了,但由于某种原因,无论我的参数是否满足,这个函数总是返回true.这导致程序在第一次移动后结束.为什么这样做,我该如何解决?

bool checkwin( Boardstatus& BSTAT)
{
    //Row 'A' checkwin
    if (BSTAT.getsquarestatus("A1")==BSTAT.getsquarestatus("A2") && BSTAT.getsquarestatus("A2")==BSTAT.getsquarestatus("A3"))
    {
        return true;
    }
    //Row 'B' checkwin
    else if (BSTAT.getsquarestatus("B1")==BSTAT.getsquarestatus("B2") && BSTAT.getsquarestatus("B2")==BSTAT.getsquarestatus("B3"))
    {
        return true;
    }
    //Row 'C' checkwin
    else if (BSTAT.getsquarestatus("C1")==BSTAT.getsquarestatus("C2") && BSTAT.getsquarestatus("C2")==BSTAT.getsquarestatus("C3"))
    {
        return true;
    }
    //Column 1 checkwin
    else if (BSTAT.getsquarestatus("A1")==BSTAT.getsquarestatus("B1") && BSTAT.getsquarestatus("B1")==BSTAT.getsquarestatus("C1"))
    {
        return true;
    }
    //Column 2 checkwin
    else if (BSTAT.getsquarestatus("A2")==BSTAT.getsquarestatus("B2") && BSTAT.getsquarestatus("B2")==BSTAT.getsquarestatus("C2"))
    {
        return true;
    }
    //Column 3 checkwin
    else if (BSTAT.getsquarestatus("A3")==BSTAT.getsquarestatus("B3") && BSTAT.getsquarestatus("B3")==BSTAT.getsquarestatus("C3"))
    {
        return true;
    }
    //Diagonal upper-left->bottom-right …
Run Code Online (Sandbox Code Playgroud)

c++ boolean tic-tac-toe

-4
推荐指数
1
解决办法
129
查看次数

如何打开和关闭更改开关的类(SwitchBoard)?

我想用以下属性构建一个类SwitchBoard:

•当我创建交换机时,我应该能够设置它包含的交换机数量.

•所有开关应从"关闭"位置开始.

•如果我打印配电盘,它应该按以下方式打印:"以下开关打开:0 2 4 6 8".

•哪种切换方法应按顺序返回表示开启的整数列表(例如,[1,3,5,7,9]).

•如果我用n作为整数调用flip(n),它应该翻转第n个lightswitch的状态.

•如果我用n作为整数调用每个(n)翻转,它应该翻转每个第n个光开关的状态,从0开始.所以翻转每(2)将翻转开关0,2,4,6等.

•方法reset()应关闭所有开关.

•如果我要求交换机翻转不存在的交换机,则不应发生任何事情(它不应该崩溃)

所以,我首先构建一个运行良好的Lightswitch.

class LightSwitch():
''' A class to reprenset a general light switch'''

    def __init__(self, light_state):
        if(light_state == 'on'):
            mode = True
        else:
            mode = False
        self._state = mode

    def __str__(self):
        if(self._state == True):
           return 'I am on'
        if(self._state == False):
            return 'I am off'

    def turn_on(self):
        if(self._state == False):
            self._state = not self._state

    def turn_off(self):
        if(self._state == True):
            self._state = not self._state

    def flip(self):
        self._state = not …
Run Code Online (Sandbox Code Playgroud)

python boolean class function

-4
推荐指数
1
解决办法
903
查看次数

变量在切换情况下不起作用

当我尝试编译以下内容时:

bool matrix[h][w];

bool c = 0;
switch(1) // was close?
{
  case matrix[y][x-1]: // up 1
  case matrix[y-2][x-1]: // down 1
  case matrix[y-1][x]: // right 1
  case matrix[y][x-2]: // left 1
    c = 1;
    break;
}
Run Code Online (Sandbox Code Playgroud)

它回来了the value of 'matrix' is not usable in a constant expression.我究竟做错了什么?

c++ arrays boolean

-4
推荐指数
1
解决办法
82
查看次数

字符串未被识别为有效的布尔异常

我正在将dataRow值转换为布尔值但得到以下异常:

字符串未被识别为有效的布尔值

这是代码:

bool a = Convert.ToBoolean(row["ISMOVING"].ToString());
Run Code Online (Sandbox Code Playgroud)

row["ISMOVING"] 包含1

我究竟做错了什么?

c# boolean tostring type-conversion

-4
推荐指数
1
解决办法
3230
查看次数

带有for循环的Java布尔方法

嘿我正在尝试在一个应该返回布尔值的方法中执行for循环.但是我不断收到错误.基本上,阵列可以变得非常大,我希望通过整个阵列检查以查找用户名和密码.

public class Users {
    private String username;
    private String password;
    private String[][] accounts = { { "user1", "pass1" }, { "user2", "pass2" } };

    public Users(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public boolean check() {

        for (int i = 0; i < accounts.length; i++) {
            if ((username.equals(accounts[i][0])) && (password.equals(accounts[i][1]))) 
                return true;
            else 
                return false;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

java arrays for-loop boolean

-4
推荐指数
1
解决办法
2624
查看次数