[英]why java program is outputting the following
Below is the code for implementing time server 下面是实现时间服务器的代码
Server class 服务器类
public static void main(String args[])
{
try{
ServerSocket ss=new ServerSocket(990);
Socket s=ss.accept();
while(true)
{
Calendar c=Calendar.getInstance();
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out =new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
out.println("Hello This is server & My Time is :");
out.println("Time ::: Hour="+c.HOUR +" Min="+c.MINUTE +" sec="+c.SECOND);
out.flush();
s.close();
}
}
catch(Exception e)
{
}
}
Client class 客户类
public static void main(String args[])
{
try{
ServerSocket ss=new ServerSocket(990);
Socket s=ss.accept();
while(true)
{
Calendar c=Calendar.getInstance();
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out =new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
out.println("Hello This is server & My Time is :");
out.println("Time ::: Hour="+c.HOUR +" Min="+c.MINUTE +" sec="+c.SECOND);
out.flush();
s.close();
}
}
catch(Exception e)
{
}
}
the program is working but the output is always 程序正在运行,但输出始终
time:: hour=10 min=12 sec=13
why it is outputting the above values 为什么输出上述值
That's because you're printing the values of the static integer fields called HOUR
, MINUTE
, SECOND
present in the Calendar
class. 那是因为您要打印
Calendar
类中存在的称为HOUR
, MINUTE
, SECOND
的静态整数字段的值。 You need to use the Calendar#get(field)
method to get the HOUR
, MINUTE
, SECOND
values from the Calendar. 您需要使用
Calendar#get(field)
方法从Calendar获取HOUR
, MINUTE
, SECOND
值。
out.println("Time ::: Hour="+c.get(Calendar.HOUR) +" Min="+c.get(Calendar.MINUTE)+" sec="+c.get(Calendar.SECOND));
Note that since HOUR, MINUTE, SECOND are static
fields, you need to access them using the class name( Calendar.SECOND
) and not using the instance( c.SECOND
). 请注意,由于HOUR,MINUTE,SECOND是
static
字段,因此您需要使用类名Calendar.SECOND
而不是实例c.SECOND
来访问它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.