㈠ 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();
}
}