资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java设定人数代码 java怎么设置参数

java 怎么实现限制用户在线人数,求实例

锁加单例:

平度网站建设公司成都创新互联,平度网站设计制作,有大型网站制作公司丰富经验。已为平度近1000家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的平度做网站的公司定做!

public class Regist{

private static Regist rgst=new Regist();

private Regist(){}

private int cnt=0;

private int max=100;

public static getRegist(){return rgst;}

///设置最大上线人数

public void setMax(int max){this.max=max;}

///判断新用户能否登入

public synchronized boolean login(){

if(cnt=max)return false;

++cnt;

return true;

}

///用户退出时调用

public synchronized void logout(){

--cnt;

}

};

有新用户要登入时,先Regist::getRegist().login()判断能否登入,返回true则表示没到用户上限,可以登入,否则表示到上限了,不能登入,用户退出时需要调用Regist::getRegist().logout()

synchronized不清楚可以百度,是种类似锁的东西。作用于方法上,相当于调用方法前先lock,调用之后unlock,其锁的对象是该方法的类对象。

线程不是太多可以用这个方法,线程太多用这个可能慢了些,好像加锁比较耗时间,如果同一个时间太多人进行登入操作,都得在那等着,一个一个进行,反应慢

JAVA编程利用一维数组,统计一个班学生0-9……90-99及100各分数段的人数。该怎么做?

public static void main(String[] args)

{

int a[]=new int[10];

Scanner sc = new Scanner(System.in);

System.out.println("请输入十个数字0~100");

for(int i=0;i10;i++)

{

System.out.println("输入第"+(i+1)+"数:");

a[i]=sc.nextInt();

}

int b[]=new int[11];

for(int i=0;i11;i++)

{

b[i]=0;

}

for(int i=0;i10;i++)

{

if(a[i]=0a[i]=9)

{

b[0]++;

}

else if(a[i]=10a[i]=19)

{

b[1]++;

}

else if(a[i]=20a[i]=29)

{

b[2]++;

}

else if(a[i]=30a[i]=39)

{

b[3]++;

}

else if(a[i]=40a[i]=49)

{

b[4]++;

}

else if(a[i]=50a[i]=59)

{

b[5]++;

}

else if(a[i]=60a[i]=69)

{

b[6]++;

}

else if(a[i]=70a[i]=79)

{

b[7]++;

}

else if(a[i]=80a[i]=89)

{

b[8]++;

}

else if(a[i]=90a[i]=99)

{

b[9]++;

}

else if(a[i]==100)

{

b[10]++;

}

}

System.out.println("0~9人数:"+b[0]+";10~19人数:"+b[1]+";20~29人数:"+b[2]+";30~39人数:"+b[3]+";40~49人数:"+b[4]+";50~59人数:"+b[5]+";60~69人数:"+b[6]+";70~79人数:"+b[7]+";80~89人数:"+b[8]+";90~99人数:"+b[9]+";100人数:"+b[10]);

for(int i=0;i11;i++)

{

System.out.println("第"+(i+1)+"段人数为:"+b[i]);

}

}

简单的方法是:

public static void main(String[] args)

{

int a[]=new int[11];

Scanner sc = new Scanner(System.in);

System.out.println("请输入十个数字0~100");

for(int i=0;i10;i++)

{

System.out.println("输入第"+(i+1)+"数:");

a[sc.nextInt()/10]++;//对于这种根据读取数据分段的数据利用int型数据整除可以很好的分出每个段的个数;

}

for(int i=0;i11;i++)

{

System.out.println("第"+(i+1)+"段人数为:"+a[i]);

}

}

JAVA如何统计人数(接着下面的程序写)

在类中声明一个静态变量如下 :

static int num=0 ,每次类初始化将num 加一。

静态变量为所有类共享,num的大小即为创建的student对象的数目

程序稍微改动如下:

class student {

String name;

static int num =0;

String id;

float ywscore;

float sxscore;

float sumscore;

float avgscore;

student(String name,String id,float ywscore,float sxscore){

this.name=name;

this.id=id;

this.ywscore=ywscore;

this.sxscore=sxscore;

num ++;

}

float sum(){

sumscore=sxscore+ywscore;

return sumscore;

}

float avg(){

avgscore=(sxscore+ywscore)/2;

return avgscore;

}

int getNum()

{

return num;

}

void showinfo(){

System.out.println("name: "+name);

System.out.println("id: "+id);

System.out.println("mathscore: "+sxscore);

System.out.println("langue: "+ywscore);

System.out.println("avg: "+avg());

System.out.println("sum: "+sum());

}

};

java在线人数问题

首先给你讲一下,我的理解,人数统计好说,因为,可已设置监听器,让其在服务器启动的时候进行监听,然后统计登录人数,但是统计在线人数,就麻烦点了,因为大多数人都不愿意点击注销按钮,直接关闭浏览器,这样session的关闭就麻烦了点,我先给出统计人数的代码,我想统计在线人数可以定时调用程序来统计,但是不是实时的,所以最大的障碍就是怎么在关闭浏览器的时候,就关闭session

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

import javax.servlet.ServletContext;

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

public class ApplicationListener implements ServletContextListener{

public void contextDestroyed(ServletContextEvent arg0) {

ServletContext sc = arg0.getServletContext();

Integer count = (Integer) sc.getAttribute("AccessCount");

try {

PrintWriter out = new PrintWriter(new FileWriter("c:/Users/god/count.txt"));// 将人数保存到文本文件

System.out.println("count:"+count);

out.print(count);

out.flush();

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

public void contextInitialized(ServletContextEvent arg0) {

Integer count = 0;

ServletContext sc = arg0.getServletContext();

try {

Scanner in = new Scanner(new FileReader("c:/Users/god/count.txt"));// 读取已经产生的人数信息,然后进行累加

count = in.nextInt();

in.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

sc.setAttribute("AccessCount", count);

}

}

在web.xml文件里面进行设置

listener

listener-class

文件的位置也就是包名.ApplicationListener

/listener-class

/listener

统计的时候是设置拦截器,需要在struts.xml中设置

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

@SuppressWarnings("serial")

public class AccessInterceptor implements Interceptor{

private String id;

public void destroy() {

}

public void init() {

}

@SuppressWarnings("unchecked")

public String intercept(ActionInvocation ai) throws Exception {

ServletContext sc = ServletActionContext.getServletContext();

HttpServletRequest request = ServletActionContext.getRequest();

HttpSession session = request.getSession();

if(!session.getId().equals(id)){

Integer count = (Integer) sc.getAttribute("AccessCount");// 判断如果登录成功,统计数字自增

sc.setAttribute("AccessCount", count+1);

id = session.getId();

}

String result = ai.invoke();

return result;

}

}


当前名称:java设定人数代码 java怎么设置参数
本文网址:http://cdkjz.cn/article/ddsjhhc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220