计数器java
① 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>
<title>Test Application</title>
</head>
<body>
<center>
<form action="test.jsp">
<input type="submit" value="点一次就加一次">
</form>
</center>
</body>
</html>
② 关于JAVA累加计数器。
因为你不是在sum%7==0这个条件下执行count++的,所以这样写就行了:
int count = 0 ;
for (int sum=1;sum<=100 ; sum++)
{
if (sum % 7 == 0)
{
System.out.println("sum="+sum);
count++;
}
}
System.out.println("count="+count);
③ 用java编写一个计数器或计时器
importjava.awt.BorderLayout;
importjava.awt.Container;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
{
=201306211111L;
privateJTextFieldscreen=newJTextField("0");
privateJButtonstart=newJButton("开始");
privateJButtonreset=newJButton("重置");
privateJPanelpanel=newJPanel();
privatebooleanisRunning;
privateinttime;
privateinttimeBetween;
publicTimerDemo(inttimeBetween){
super("计时器");
this.timeBetween=timeBetween;
try{
init();
}catch(Exceptione){
e.printStackTrace();
}
}
publicTimerDemo(){
super("计时器");
this.timeBetween=100;
try{
init();
}catch(Exceptione){
e.printStackTrace();
}
}
privatevoidinit(){
panel.setLayout(newGridLayout());
panel.add(start);
panel.add(reset);
start.addActionListener(this);
reset.addActionListener(this);
screen.setFont(newFont("幼圆",Font.BOLD,60));
screen.setHorizontalAlignment(JTextField.CENTER);
screen.setEditable(false);
Containerc=getContentPane();
c.setLayout(newBorderLayout());
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);
}
publicstaticvoidmain(String[]args){
newTimerDemo(1);//设定1ms/次
//newTimerDemo();
}
@Override
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==start){
if(start.getText().equals("开始")){
start.setText("暂停");
isRunning=true;
}elseif(start.getText().equals("暂停")){
start.setText("开始");
isRunning=false;
}
}
if(e.getSource()==reset){
start.setText("开始");
screen.setText("0");
isRunning=false;
time=0;
}
newThread(newTimeZone()).start();
}
{
@Override
publicvoidrun(){
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(Exceptione){
e.printStackTrace();
}
}
}
}
}
④ java 如何设置计数器 就是打印了多少回文字 还有设置每行只打印7个回文字
public class text1 {
public static void main(String[] args) {
int g,shi,,qian;
int x = 0;
for (int a = 1000;a <= 9999;a++) {
g = a % 10;
shi =a/ 10 % 10;
= a/100%10;
qian = a / 1000;
if(g + shi == + qian) {
x++;
System.out.print(a);
if(x % 7 == 0)
System.out.println();
else
System.out.print(" ");
}
}
System.out.println();
System.out.println("共:"+x+"个!");
}
}
⑤ 如何用JAVA写一个计数器让它能自动按顺序产生学号或员工编号这类的资料呢
你的这个问题其实挺有意思的,你先想一下你这个表打算怎么设计。
首先你先要设计一个年级班级表,比如年级表就是2015.2016.2015级。然后班级表。计算机系
01软件工程02应用化学03。不细分了,就是按系就是班了。
那么学生表就是
idstu_idgreedclassname.....其他信息
主键学号年级班级名字其他
大概就这个简单设计下。greed和class在其他的表都有这个东西,所以就根据这两个的规则来
生成学生的学号。在录入的时候先查已经入库的这个年纪班级下的所有记录。如果有就有一个
学号的id比如201501110这个。这样再新增就给下一个学生+1。如果没有,那么就从
201501001开始往上加。
这种问题其实你可以想一下你们学校的学号是怎么设计的。
⑥ 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代码实现一个计数器类(Computer),其中包括:变量value初始值为0
classComputer{
intvalue;
Computer(intvalue){
this.value=value;
}
publicvoidadd(){
System.out.println("Value:"+value+"->"+(value+1));
value++;
}
publicvoidsub(){
System.out.println("Value:"+value+"->"+(value-2));
value-=2;
}
publicvoidclear(){
System.out.println("Value:"+value+"->"+0);
value=0;
}
}
publicclassDemo{
publicstaticvoidmain(String[]args){
Computercomputer=newComputer(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 类的对象的计数器
一楼用System.gc();来调用finalize()方法是不确定的,得到的结果是不稳定的。最好定义一个static类型的方法,来销毁类例如:
public class Test {
public static int num = 0;
public Test() {
num ++;
System.out.println("NO=" + num);
}
//静态删除方法,用类名可以直接调用
public static void delete(Test o) {
num --;
System.out.println("NO=" + num);
o=null;
}
public static void main(String [] args) {
Test [] objs = new Test[5];
for(Test obj: objs) {
obj = new Test();
}
Test.delete(objs[1]);
Test.delete(objs[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
}