classstudent{Stringname;Stringid;floatywscore;floatsxscore;floatsumscore;floatavgscore;student(Stringname,Stringid,floatywscore,floatsxscore){this.name=name;this.id=id;th...
槐蔭ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
class student {
String name;
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;
}
float sum(){
sumscore=sxscore+ywscore;
return sumscore;
}
float avg(){
avgscore=(sxscore+ywscore)/2;
return avgscore;
}
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());
}
};
public class text {
public static void main(String [] args){
student s=new student("McGrady","01",90,99);
s.showinfo();
}
}創(chuàng)建多個(gè)STUDENT對(duì)象 統(tǒng)計(jì)student的個(gè)數(shù)!
在類中聲明一個(gè)靜態(tài)變量如下 :
static int num=0 ,每次類初始化將num 加一。
靜態(tài)變量為所有類共享,num的大小即為創(chuàng)建的student對(duì)象的數(shù)目
程序稍微改動(dòng)如下:
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());
}
};
1.用session超時(shí),session為null就表示下線了
2.也可以采用數(shù)據(jù)庫(kù)中設(shè)置 臨時(shí)表 來處理
一個(gè)用戶登陸時(shí)向表中插進(jìn)一條記錄,用戶離開時(shí)候刪除該記錄
如想統(tǒng)計(jì)在線人數(shù),簡(jiǎn)單地執(zhí)行
select count(*) from table... 即可
3.application對(duì)象中可以記住現(xiàn)在的人數(shù),application的生命周期和服務(wù)器的生命周期一樣長(zhǎng)。
4.還有一種方法要用到一個(gè)文件global.jsa ,方法是(在JSP中)是sessionDestroy(),其中它是以session對(duì)象為參數(shù)的。還有要把global.jsa文件必須房子和JSP程序相同的文件目錄內(nèi)才行。
5.網(wǎng)頁(yè)自動(dòng)刷新的代碼是:
在文件頭部加上
meta http-equiv="refresh" content="15"
刷新間隔時(shí)間是15秒
6.在session中加入監(jiān)聽類,類的示例代碼如下:
onLineUser.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.util.*;
public class onLineUser implements HttpSessionBindingListener {
public onLineUser(){
}
private Vector users=new Vector();
public int getCount(){
users.trimToSize();
return users.capacity();
}
public boolean existUser(String userName){
users.trimToSize();
boolean existUser=false;
for (int i=0;iusers.capacity();i++ )
{
if (userName.equals((String)users.get(i)))
{
existUser=true;
break;
}
}
return existUser;
}
public boolean deleteUser(String userName) {
users.trimToSize();
if(existUser(userName)){
int currUserIndex=-1;
for(int i=0;iusers.capacity();i++){
if(userName.equals((String)users.get(i))){
currUserIndex=i;
break;
}
}
if (currUserIndex!=-1){
users.remove(currUserIndex);
users.trimToSize();
return true;
}
}
return false;
}
public Vector getOnLineUser()
{
return users;
}
public void valueBound(HttpSessionBindingEvent e) {
users.trimToSize();
if(!existUser(e.getName())){
users.add(e.getName());
System.out.print(e.getName()+"\t 登入到系統(tǒng)\t"+(new Date()));
System.out.println(" 在線用戶數(shù)為:"+getCount());
}else
System.out.println(e.getName()+"已經(jīng)存在");
}
public void valueUnbound(HttpSessionBindingEvent e) {
users.trimToSize();
String userName=e.getName();
deleteUser(userName);
System.out.print(userName+"\t 退出系統(tǒng)\t"+(new Date()));
System.out.println(" 在線用戶數(shù)為:"+getCount());
}
}
jsp:
%@ page contentType="text/html;charset=gb2312" %
%@ page import="java.util.*" %
jsp:useBean id="onlineuser" class="temp.jb.onLineUser" scope="application"/
html
head
/head
body onUnload="postMessage()"
center
ph1登陸成功,歡迎訪問/h1/p
/center
% session = request.getSession(false); %
%
String username=request.getParameter("username");
if (onlineuser.existUser(username)){
out.println("用戶font color=red"+username+"/font已經(jīng)登陸!");
}else{
session.setMaxInactiveInterval(50); //Sesion有效時(shí)長(zhǎng),以秒為單位
session.setAttribute(username,onlineuser);
out.println("歡迎新用戶:font color=red"+username+"/font登陸到系統(tǒng)!");
}
out.println("br當(dāng)前在線用戶人數(shù):font color=red"+onlineuser.getCount()+"/fontbr");
String ip = request.getRemoteAddr();
out.println("brIP:font color=red"+ip+"/fontbr");
Vector vt=onlineuser.getOnLineUser();
Enumeration e = vt.elements();
out.println("在線用戶列表");
out.println("table border=1");
out.println("trtd用戶名/td/tr");
while(e.hasMoreElements()){
out.println("trtd");
out.println((String)e.nextElement()+"br");
out.println("/td/tr");
}
out.println("/table");
%
center
p /p
[a href="javascript:window.close()"關(guān)閉窗口/a]
%
out.println("pa href='logout.jsp?username="+username+"'退出系統(tǒng)/a/p");
%
/center
Script
function postMessage(){
%onlineuser.deleteUser(request.getParameter("username"));%
}
/Script
/body
/html
public static void main(String[] args)
{
int a[]=new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入十個(gè)數(shù)字0~100");
for(int i=0;i10;i++)
{
System.out.println("輸入第"+(i+1)+"數(shù):");
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人數(shù):"+b[0]+";10~19人數(shù):"+b[1]+";20~29人數(shù):"+b[2]+";30~39人數(shù):"+b[3]+";40~49人數(shù):"+b[4]+";50~59人數(shù):"+b[5]+";60~69人數(shù):"+b[6]+";70~79人數(shù):"+b[7]+";80~89人數(shù):"+b[8]+";90~99人數(shù):"+b[9]+";100人數(shù):"+b[10]);
for(int i=0;i11;i++)
{
System.out.println("第"+(i+1)+"段人數(shù)為:"+b[i]);
}
}
簡(jiǎn)單的方法是:
public static void main(String[] args)
{
int a[]=new int[11];
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入十個(gè)數(shù)字0~100");
for(int i=0;i10;i++)
{
System.out.println("輸入第"+(i+1)+"數(shù):");
a[sc.nextInt()/10]++;//對(duì)于這種根據(jù)讀取數(shù)據(jù)分段的數(shù)據(jù)利用int型數(shù)據(jù)整除可以很好的分出每個(gè)段的個(gè)數(shù);
}
for(int i=0;i11;i++)
{
System.out.println("第"+(i+1)+"段人數(shù)為:"+a[i]);
}
}
public?class?ScoreDemo?{
public?static?void?main(String[]?args)?{
int[]?score?=?{89,75,36,57,56,82,73,60,72};//一組成績(jī)
int?line?=?60;//及格分?jǐn)?shù)線
int?nums=0;//此變量用于保存不及格的人數(shù)
for?(int?i?=?0;?i??score.length;?i++)?{//循環(huán)遍歷
if(score[i]line){//如果低于分?jǐn)?shù)線
nums++;//不及格的人數(shù)加一個(gè)
}
}
System.out.println("不及格的人數(shù)"+nums+"人");
}
}
輸出
不及格的人數(shù)3人