您的位置:

java计数器,java计数器实现原理

本文目录一览:

试编写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

采纳哦

用java编写一个计数器或计时器

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class TimerDemo extends JFrame implements ActionListener {

    private static final long serialVersionUID = 201306211111L;

    private JTextField screen = new JTextField("0");

    private JButton start = new JButton("开始");

    private JButton reset = new JButton("重置");

    private JPanel panel = new JPanel();

    private boolean isRunning;

    private int time;

    private int timeBetween;

    public TimerDemo(int timeBetween) {

        super("计时器");

        this.timeBetween = timeBetween;

        try {

            init();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public TimerDemo() {

        super("计时器");

        this.timeBetween = 100;

        try {

            init();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    private void init() {

        panel.setLayout(new GridLayout());

        panel.add(start);

        panel.add(reset);

        start.addActionListener(this);

        reset.addActionListener(this);

        screen.setFont(new Font("幼圆", Font.BOLD, 60));

        screen.setHorizontalAlignment(JTextField.CENTER);

        screen.setEditable(false);

        Container c = getContentPane();

        c.setLayout(new BorderLayout());

        c.add(panel, BorderLayout.SOUTH);

        c.add(screen, BorderLayout.CENTER);

        this.setSize(200, 150);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setResizable(false);

        this.setLocationRelativeTo(null);

        this.setVisible(true);

    }

    public static void main(String[] args) {

        new TimerDemo(1);// 设定 1ms/次

        // new TimerDemo();

    }

    @Override

    public void actionPerformed(ActionEvent e) {

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

            if (start.getText().equals("开始")) {

                start.setText("暂停");

                isRunning = true;

            } else if (start.getText().equals("暂停")) {

                start.setText("开始");

                isRunning = false;

            }

        }

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

            start.setText("开始");

            screen.setText("0");

            isRunning = false;

            time = 0;

        }

        new Thread(new TimeZone()).start();

    }

    class TimeZone implements Runnable {

        @Override

        public void run() {

            while (isRunning) {

                time++;

                if (time = Integer.MAX_VALUE) {

                    screen.setText("ERROR");

                    JOptionPane.showMessageDialog(null, "ERROR");

                    isRunning = false;

                }

                screen.setText(String.valueOf(time));

                try {

                    Thread.sleep(timeBetween);

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        }

    }

}

JAVA计数器

都什么跟什么啊?这明显就是application的概念嘛,

application: 有效范围涵盖整个应用程序,也就是对整个网站均有效,只要是这个Web应用程序下面的JSP或者Servlet,那么都能够访问到,这个application对象,你可以把内容存在application中,那么在整个Web应用程序的生命周期中都是可以拿到这个application里面的内容的,当然服务器重启,此对象被垃圾回收,必然清零了,下面是代码,页面名字叫test.jsp,应你的要求,也做了一个按钮

-------

%@ page language="java" import="java.util.*" pageEncoding="GBK"%

%@ page contentType="text/html; charset=GBK"%

%

Integer accessCount = (Integer)application.getAttribute("accessCount");

if (accessCount == null) { // 第一次访问的时候就是一个null

accessCount = new Integer(0);

} else {

accessCount = new Integer(accessCount.intValue() + 1);

}

application.setAttribute("accessCount", accessCount);

out.println("font size='20' color='red'"+accessCount+"/font");

%

html

head

titleTest Application/title

/head

body

center

form action="test.jsp"

input type="submit" value="点一次就加一次"

/form

/center

/body

/html

java程序计数器存的什么

java中的程序计数器,确切的来说是jvm中的程序计数器:程序计数器是一块较小的内存空间,它的作用可以看作是当前线程所执行的字节码的行号指示器,内存中的一块空间

而 指向下一条指令地址 这个程序计数器,是指的cpu中的程序计数器,是硬件层面的东西,是计算机处理器中的寄存器,