首页 > 编程知识 正文

求一个java编程计数器的代码,计算器编程代码java

时间:2023-12-28 21:11:15 阅读:329472 作者:YYRT

本文目录一览:

求一个JAVA编程计数器的代码!

什么技术器?

网站流量还是网页计数器?

=======================以下是一个js计数器=======================

SCRIPT language="JavaScript"

!--

function www_webjx_com(offset)

{

var endstr=document.cookie.indexOf(";",offset);if(endstr==-1)

endstr=document.cookie.length;return unescape(document.cookie.substring(offset,endstr));}

function GetCookie(name)

{

var arg=name+"=";

var alen=arg.length;

var clen=document.cookie.length;

var i=0;while(iclen)

{

var j=i+alen;

if(document.cookie.substring(i,j)==arg)

return www_webjx_com(j);

i=document.cookie.indexOf(" ",i)+1;if(i==0)

break;

}

return null;

}

function SetCookie(name,value)

{

var argv=SetCookie.arguments;

var argc=SetCookie.arguments.length;

var expires=(2argc)?argv[2]:null;

var path=(3argc)?argv[3]:null;

var domain=(4argc)?argv[4]:null;

var secure=(5argc)?argv[5]:false;

document.cookie=name+"="+escape(value)+((expires==null)?"":("; expires="+expires.toGMTString()))+((path==null)?"":("; path="+path))+((domain==null)?"":("; domain="+domain))+((secure==true)?"; secure":"");

}

var expdate=new Date();

var visits;

expdate.setTime(expdate.getTime()+(24*60*60*1000*365)); //设置COOKIES时间为1年,自己随便设置该时间

if(!(visits=GetCookie("visits")))

visits=0;visits++;SetCookie("visits",visits,expdate,"/",null,false);

//以下信息显示可以使用标准的HTML语法,自己随便设置

document.write("你已经光临本页"+"FONT COLOR=red"+visits+"/FONT"+"次!");

if(visits==1)//来访1次的信息显示

document.write("br"+"欢迎光临本站,觉得本站内容如何?");

if(visits==2)//来访2次的信息显示,以下类推,自己随便增加

document.write("br"+"再次光临不胜容幸!今后要常来啊!欢迎点击本站广告。");

if(visits==3)

document.write("br"+"常客,在本站的论坛发个帖子如何?");

if(visits==4)

document.write("br"+"你这个疯子!");

if(visits=5)

document.write("br"+"疯狂的家伙!我真的非常喜欢你。");

//--

/SCRIPT

优点:js,直接粘贴至html中就可以了。

缺点:是页面计数计数,计算某个人访问某个网页的次数,不是网站的访问量,如果你要的是网站访问量统计,你留下qq/msn我发文件给你。

java计数器

参考下面代码:

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class Test extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;

private JLabel lbl;

private JButton btn1;

private JButton btn2;

private JButton btn3;

private int con;

public static void main(String args[]) {

try {

Test test = new Test();

test.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

public Test() {

super();

getContentPane().setLayout(null);

setTitle("Test");

setName("");

setResizable(false);

setBounds(100, 100, 300, 216);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

lbl = new JLabel();

lbl.setText(String.valueOf(con));

lbl.setBounds(84, 58, 96, 25);

getContentPane().add(lbl);

btn1 = new JButton();

btn1.setText("+ 1");

btn1.setBounds(29, 106, 64, 26);

btn1.addActionListener(this);

getContentPane().add(btn1);

btn2 = new JButton();

btn2.setText("- 1");

btn2.setBounds(99, 106, 64, 26);

btn2.addActionListener(this);

getContentPane().add(btn2);

btn3 = new JButton();

btn3.setText("Clear");

btn3.setBounds(169, 106, 64, 26);

btn3.addActionListener(this);

getContentPane().add(btn3);

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == btn1) {

con = Integer.parseInt(lbl.getText());

con++;

lbl.setText(String.valueOf(con));

}

if (e.getSource() == btn2) {

con = Integer.parseInt(lbl.getText());

con--;

lbl.setText(String.valueOf(con));

}

if (e.getSource() == btn3) {

lbl.setText(String.valueOf(0));

}

}

}

计数器的java代码

看书觉得很容易,真正写代码才发现真不容易,累死。

我也是JAVA初学者(学了不到半年),代码肯定有不合适的地方,凑合看吧,反正功能是完成了,代码如下:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TestClock extends JFrame{

/** Creates a new instance of TestClock */

public TestClock() {

JPanel jp=new JPanel();

final JLabel jl=new JLabel("0");

jp.add(jl);

add(jp,BorderLayout.CENTER);

JButton jbStart=new JButton("开始");

jbStart.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JButton j =(JButton)e.getSource();

j.setEnabled(false);

dt=new DamThread(new ClockThread(jl));

dt.start();

}

});

JButton jbPause=new JButton("暂停");

jbPause.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JButton j=(JButton)e.getSource();

String s=(String)e.getActionCommand();

if(s.equals("暂停")){

dt.setStatus(ClockStatus.PAUSE);

j.setText("继续");

}else{

dt.setStatus(ClockStatus.CONTINUE);

j.setText("暂停");

}

}

});

JButton jbZero=new JButton("清零");

jbZero.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

dt.setStatus(ClockStatus.ZERO);

}

});

JButton jbStop=new JButton("停止");

jbStop.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

dt.setStatus(ClockStatus.STOP);

}

});

JPanel jp1=new JPanel();

jp1.add(jbStart);

jp1.add(jbPause);

jp1.add(jbZero);

jp1.add(jbStop);

add(jp1,BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pack();

setLocationRelativeTo(null);

}

public static void main(String[] args) {

TestClock tc=new TestClock();

tc.setVisible(true);

}

DamThread dt;

}

class DamThread extends Thread{

public DamThread(ClockThread c){

this.ct=c;

ct.start();

this.setDaemon(true);

this.STATUS=ClockStatus.START;

}

public void run(){

while(ct.isAlive()){

CheckStatus();

}

}

private void CheckStatus(){

switch(getStatus()){

case PAUSE:

ct.mysuspend();

break;

case ZERO:

ct.seti(0);

ct.label.setText("0");

setStatus(ClockStatus.START);

break;

case STOP:

ct.seti(1001);

break;

case CONTINUE:

ct.myresume();

break;

default:

break;

}

}

public void setStatus(ClockStatus cs){

this.STATUS=cs;

}

public ClockStatus getStatus(){

return STATUS;

}

ClockStatus STATUS;

ClockThread ct;

}

class ClockThread extends Thread{

public ClockThread(JLabel j){

this.label=j;

suspendFlag=false;

}

public void run(){

while(i=1000){

try {

i++;

label.setText(""+i);

synchronized(this){

while(suspendFlag){

wait();

}

}

sleep(100);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

public void seti(int in){

this.i=in;

}

public void mysuspend()

{

suspendFlag=true;

}

synchronized void myresume()

{

suspendFlag=false;

notify();

}

private boolean suspendFlag;

private int i=0;

JLabel label;

}

enum ClockStatus{

START,PAUSE,ZERO,STOP,CONTINUE

}

试编写java代码实现一个计数器类(Computer),其中包括:变量value初始值为0

class Computer{

    int value;

    Computer(int value){

        this.value=value;

    }

    public void add(){

        System.out.println("Value:"+value+"-"+(value+1));

        value++;

    }

    public void sub(){

        System.out.println("Value:"+value+"-"+(value-2));

        value-=2;

    }

    public void clear(){

        System.out.println("Value:"+value+"-"+0);

        value=0;

    }

}

public class Demo{

    public static void main(String[] args){

        Computer computer=new Computer(10);

        computer.add();

        computer.sub();

        computer.clear();

    }

}

JAVA编写一个完整的计数器类Count,写出源代码

public class Count{ int countValue; Count(){ countValue=0; } public void increment() { countValue++; } public void decrement() { countValue--; } public void reset() { countValue=0; } public int getCountValue(){ return countValue; } public static void main(String args[]){ Count c = new Count(); c.increment(); System.out.println(c.getCountValue()); c.reset(); System.out.println(c.getCountValue()); } } 运行结果: 1 0

采纳哦

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。