㈠ java如何用static來計數舉個例子把
public class frame {
static int count = 0;
public frame(){
count++;
}
public static void main(String args[]){
frame i1 = new frame();
frame i2 = new frame();
frame i3 = new frame();
frame i4 = new frame();
System.out.println(count);
System.out.println(frame.count);
}
}
使用count來統計創建出內了多少個容類的實例
㈡ 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 計數問題,超過11位數就出錯為什麼
你代碼中使用的in.nextInt()來獲取控制台輸入,而且是以int類型接收
在java中int類型長度4位元組,理論支持的最大值是罩帆2的32次方慶梁-1(2147483647)
你在控制台輸入的數值遠超過這個數,這個值是無法轉換成一個int的物差雹,所以報錯
並不是你輸入的內容超過11位就報錯,而是你輸入的值大於2147483647就會報錯
㈣ 用java編寫一個計數器或計時器
import java.io.IOException;
import java.util.Timer;
public class TimerTest {
public static void main(String[] args){
Timer timer = new Timer();
timer.schele(new MyTask(), 1000, 2000);//在1秒後執行此任務,每次間隔2秒,如果傳遞一個Data參數,就可以在某個固定的時間執行這個任務.
while(true){//這個是用來停止此任務的,否則就一直循環執行此任務了
try {
int ch = System.in.read();
if(ch-'c'==0){
timer.cancel();//使用這個方法退出任務
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static class MyTask extends java.util.TimerTask{
@Override
public void run() {
//你要進行的操作
}
}
}
㈤ java用循環編寫一個計數程序輸入幾個數字直到輸入0為止,計算輸入的這些數的平均數
參考代碼如下:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
int count = 0;
while(true){
int num = sc.nextInt();
if(num == 0) break;
sum += num;
count++;
}
System.out.println("平均值:"+sum*1.0/count);
}
}
運行結果:
㈥ java如何用static final做計數器
static final Map map = new HashMap();
public static void main(String u[]){
map.put("cuont",0);
for(int i=1;i<6;i++){
map.put("count",
1+Integer.valueOf((java.lang.Integer)map.get("count")));
}
}
final定義的變數不可以修改其地址,殲拍嫌但是例如上面的這種賦值氏手並未修改final對象的地賀中址,可以用來保存count對象
㈦ 求一用JAVA靜態變數Static編寫一個計數器的小程序
import java.util.Scanner;
class Count{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
System.out.println("輸入第一個計算的數字");
double a=scan.nextDouble();
System.out.println("輸入帆老瞎計算的符號 :");
scan.nextLine();//用來接收回車符含模號
String c=scan.nextLine();
System.out.println("輸入計算的第二個數態空字 :");
double b=scan.nextDouble();
char []ch=c.toCharArray();
switch(ch[0]){
case'+':System.out.println("a+b="+(a+b));break;
case'-':System.out.println("a-b="+(a-b));break;
case'*':System.out.println("a*b="+(a*b));break;
case'/':System.out.println("a/b="+(a/b));break;
default:System.out.println("你輸入的符號有誤!!");break;
}
}
}
㈧ 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靜態變數Static編寫一個計數器的小程序
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();
}
}