java倒数数字代码 java中倒数

用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编写计算器实现退格,倒数,小数点,正负号功能代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class General extends JFrame implements ActionListener

{

String args[]={"退格","总清","清零","/","1","2","3","+","4","5","6","-","7","8","9","*","0","+/-",".","="};

JButton jb[]=new JButton[20];

JPanel p1=new JPanel();

JTextField jtf=new JTextField(15);

double num1,num2,jieguo;

char c,ch;

public General()

{

Container ct=getContentPane();

jtf.addKeyListener(new KA());

jtf.setHorizontalAlignment(JTextField.RIGHT);

ct.add(jtf,"North");

p1.setLayout(new GridLayout(5,4));

for(int i=0;i20;i++)

{

jb[i]=new JButton(args[i]);

jb[i].addActionListener(this);

p1.add(jb[i]);

}

ct.add(p1);

pack();

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String args[])

{

new General();

}

public void actionPerformed(ActionEvent e)// 监听按键

{

String str=e.getActionCommand().trim();

if("0123456789.".indexOf(str)!=-1)

{

if(jtf.getText().equals("0"))jtf.setText(null);

jtf.setText(jtf.getText()+str);

return;

}

if("+-*/".indexOf(str)!=-1)

{

num1=Double.parseDouble(jtf.getText());

c=str.charAt(0);

jtf.setText(null);

return;

}

else if(str.equals("="))

{

num2=Double.parseDouble(jtf.getText());

switch(c)

{

case '+': jieguo=num1+num2; break;

case '-': jieguo=num1-num2; break;

case '*': jieguo=num1*num2; break;

case '/': jieguo=num1/num2; break;

}

jtf.setText(Double.toString(jieguo));

}

else if(str.equals("+/-"))

{

if(jtf.getText().isEmpty()==false)

{

Double lnum=-(Double.parseDouble(jtf.getText()));

jtf.setText(Double.toString(lnum));

}

else

{

jtf.setText("-");

}

}

else if(str.equals("清空"))

{

jtf.setText(null);

}

else if(str.equals("清零"))

{

jtf.setText("0");

}

else if(str.equals("退格"))

{

jtf.setText(jtf.getText().substring(0,jtf.getText().length()-1));

}

}

class KA extends KeyAdapter // 监听键盘

{

public void keyPressed(KeyEvent e)

{

char ch=e.getKeyChar();

if("+-*/".indexOf(ch)!=-1)

{

num1=Double.parseDouble(jtf.getText());

c=ch;

}

if(ch=='='|ch==e.VK_ENTER)

{

num2=Double.parseDouble(jtf.getText());

switch(c)

{

case '+': jieguo=num1+num2; break;

case '-': jieguo=num1-num2; break;

case '*': jieguo=num1*num2; break;

case '/': jieguo=num1/num2; break;

}

}

}

public void keyReleased(KeyEvent e)

{

char ch=e.getKeyChar();

if("+-*/".indexOf(ch)!=-1)

{

jtf.setText(null);

}

if(ch=='='|e.getKeyCode()==e.VK_ENTER)

{

jtf.setText(Double.toString(jieguo));

}

}

}

}

java中,1~10倒着数出来,用for语句

代码如下:

public class Test

{

public static void main(String[] args)

{

for(int i =10; i=1 ; i--)

{

System.out.print(i);

System.out.print(" ");

}

}

}

以上代码可灵活的变动for里的i初始值。

java编程求某个数的相反数,倒数,绝对值

//倒数

double y=(double)1/123;//1除以任何数 就是他本身的倒数 这里把1/123强转为double类型

System.out.println("y的倒数为"+y);

int x =123;//x可以为任意数0-x就是x的相反数

System.out.println((0-x));

//绝对值

int a =-15;

int b=Math.abs(a);

System.out.println(b);


网页标题:java倒数数字代码 java中倒数
链接地址:http://hbruida.cn/article/dohpdpg.html