我有以下代码:
static void Start()
{
First();
Second();
...
}
private bool First()
{
if()
{.....
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
可能有一个简单的解决方案,但我只想Second();在该First();方法返回 true时启动该方法
非常感谢你的帮助。
我写了一段代码
print(False>True)
print(True>False)
Run Code Online (Sandbox Code Playgroud)
结果是
False
True
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下这是怎么回事吗
我在 Go 中需要改变 bool 值并存储它。我收到 value == true 但我需要更改它并存储更改后的值。我可以想到只将警报存储到 var 并在下一个语句中传递它。
例如伪代码:
chnagevalue := false
if value == false {
changevalue == true
}
Run Code Online (Sandbox Code Playgroud)
在 Go 中最好的方法是什么?有没有预先定义的方法来做到这一点?
这段代码的输出是什么?请解释如何在此处完成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) 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个字节.
为什么会这样?
我正在制作一个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) 我想用以下属性构建一个类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) 当我尝试编译以下内容时:
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.我究竟做错了什么?
我正在将dataRow值转换为布尔值但得到以下异常:
字符串未被识别为有效的布尔值
这是代码:
bool a = Convert.ToBoolean(row["ISMOVING"].ToString());
Run Code Online (Sandbox Code Playgroud)
row["ISMOVING"] 包含1
我究竟做错了什么?
嘿我正在尝试在一个应该返回布尔值的方法中执行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)