本文目錄一覽:
- 1、試編寫java代碼實現一個計數器類(Computer),其中包括:變數value初始值為0
- 2、JAVA編寫一個完整的計數器類Count,寫出源代碼
- 3、用java編寫一個計數器或計時器
- 4、JAVA計數器
- 5、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中的程序計數器,是硬體層面的東西,是計算機處理器中的寄存器,
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/179922.html