計數器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
}