我有一个关于枚举的问题(它可能是一个简单的但......).这是我的计划:
public class Hello {
public enum MyEnum
{
ONE(1), TWO(2);
private int value;
private MyEnum(int value)
{
System.out.println("hello");
this.value = value;
}
public int getValue()
{
return value;
}
}
public static void main(String[] args)
{
MyEnum e = MyEnum.ONE;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么输出是
hello
hello
Run Code Online (Sandbox Code Playgroud)
并不是
hello
?
代码如何"两次"到构造函数?第一次是什么时候,第二次是什么时候?为什么枚举构造函数不能公开?这是它打印两次而不是一次打印的原因吗?
我对下一个代码有疑问:
int main {
double x = 0;
double y = 0/x;
if(y==1) {.....}
....
....
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我在我的计算机上运行代码时,我没有遇到运行时错误,我看到了y = -nan(0x8000000000000)
.为什么不将运行时错误除以零?
此外,当我将第一行更改为int x = 0;
现在时,存在运行时错误.有什么不同?
可能重复:
负 ASCII 值
int main() {
char b = 8-'3';
printf("%c\n",b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我运行这个程序,我得到一个看起来像问号 (?) 的标志。
我对你的问题是为什么它打印那个而不打印任何东西,因为据我所知,ASCII 表中 b 的值是负 43,这是不存在的。
顺便说一句,当我编译这段代码时:
int main() {
char b = -16;
printf("%c\n",b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我什么也得不到。
int main() {
float a = 20000000;
float b = 1;
float c = a+b;
if (c==a) { printf("equal"); }
else { printf("not equal");}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我跑这个时,它说"平等".但当我将a的值更改为2000000(少一个零)时答案是否定的.为什么?
我正在用 python 编写 2 个脚本。
客户端和服务器之间有一个套接字。场景是这样的:
我有一个客户端要求关闭程序,因此它应该通知服务器,然后它会通知另一个客户端,因此我需要关闭从客户端 (1) 到服务器的套接字,然后关闭从服务器到其他客户端的套接字(想象你自己是一个要求退出游戏的 2 人游戏)。
我就是这样做的。在 Client.py 中:
# send the request to the server
eNum, eMsg = Protocol.send_all(self.socket_to_server, msg)
if eNum:
sys.stderr.write(eMsg)
self.close_client()
self.socket_to_server.shutdown(socket.SHUT_WR)
exit(0)
Run Code Online (Sandbox Code Playgroud)
然后在Server.py的代码中:
# get the msg from the client that calleds
num, msg = Protocol.recv_all(self.players_sockets[0])
# case of failure
if num == Protocol.NetworkErrorCodes.FAILURE:
sys.stderr.write(msg)
self.shut_down_server()
# case it was disconnected
if num == Protocol.NetworkErrorCodes.DISCONNECTED:
print msg
self.shut_down_server()
technical_win = ("exit" == msg)
# send the msg to …
Run Code Online (Sandbox Code Playgroud) 我有这个代码,有一些我不明白的东西
当我编译以下代码时:
#include <stdio.h>
#include <stdlib.h>
int main() {
double x=1;
double y=0;
if (x!=y)
{
printf("x!=y\n");
}
if (x=y)
{
printf("x=y\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:警告:建议用作真值的赋值括号
当我运行程序时,我得到以下输出
x!=y
x=y
Run Code Online (Sandbox Code Playgroud)
为什么打印x = y如果'='不是要比较,而只是将值放在x中的y中.