「在java如何设置计数器」在java如何设置计数器数值
今天给各位分享在java如何设置计数器的知识,其中也会对在java如何设置计数器数值进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、JAVA编写一个完整的计数器类Count,写出源代码
- 2、如何用JAVA写一个计数器让它能自动按顺序产生学号或员工编号这类的资料呢?
- 3、用java编写一个计数器或计时器
- 4、试编写java代码实现一个计数器类(Computer),其中包括:变量value初始值为0
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写一个计数器让它能自动按顺序产生学号或员工编号这类的资料呢?
你的这个问题其实挺有意思的,你先想一下你这个表打算怎么设计。
首先你先要设计一个年级班级表,比如年级表就是2015.2016.2015 级。然后班级表。计算机系
01软件工程02应用化学03。不细分了,就是按系就是班了。
那么学生表就是
id stu_id greed class name .....其他信息
主键 学号 年级 班级 名字 其他
大概就这个简单设计下。greed和class在其他的表都有这个东西,所以就根据这两个的规则来
生成学生的学号。在录入的时候先查已经入库的这个年纪班级下的所有记录。如果有就有一个
学号的id比如2015 01 110 这个。这样再新增就给下一个学生+1 。如果没有,那么就从
201501001开始往上加。
这种问题其实你可以想一下你们学校的学号是怎么设计的。
用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代码实现一个计数器类(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如何设置计数器的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于在java如何设置计数器数值、在java如何设置计数器的信息别忘了在本站进行查找喔。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。