看书觉得很容易,真正写代码才发现真不容易,累死。
10年积累的成都做网站、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有西乡免费网站建设让你可以放心的选择与我们合作。
我也是JAVA初学者(学了不到半年),代码肯定有不合适的地方,凑合看吧,反正功能是完成了,代码如下:
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();
}
}
private void CheckStatus(){
switch(getStatus()){
case PAUSE:
ct.mysuspend();
break;
case ZERO:
ct.seti(0);
ct.label.setText("0");
setStatus(ClockStatus.START);
break;
case STOP:
ct.seti(1001);
break;
case CONTINUE:
ct.myresume();
break;
default:
break;
}
}
public void setStatus(ClockStatus cs){
this.STATUS=cs;
}
public ClockStatus getStatus(){
return STATUS;
}
ClockStatus STATUS;
ClockThread ct;
}
class ClockThread extends Thread{
public ClockThread(JLabel j){
this.label=j;
suspendFlag=false;
}
public void run(){
while(i=1000){
try {
i++;
label.setText(""+i);
synchronized(this){
while(suspendFlag){
wait();
}
}
sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public void seti(int in){
this.i=in;
}
public void mysuspend()
{
suspendFlag=true;
}
synchronized void myresume()
{
suspendFlag=false;
notify();
}
private boolean suspendFlag;
private int i=0;
JLabel label;
}
enum ClockStatus{
START,PAUSE,ZERO,STOP,CONTINUE
}
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
采纳哦
参考代码如下:
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);
}
}
运行结果: