计时器程序源代码

package Play;
public class TimeCount implements Runnable {
public void run() {
int count=0;
int ban=30;
System.out.println("----------------------计时开始----------------------");
for (int i = 0; i < 1000000; i++) {
if (i < 1000000) {

try {
Thread.sleep(1000);
System.out.println("时间已过:" + i);
if (i % 30 == 0&&i!=0&& i % 60 != 0) {//&&i!=0是为了在i=0是,不打印下面的语句,因为i % 30 == 0
System.out.println("---------------" + count + "分"
+ ban + "秒---------------");
}
if (i % 60 == 0&&i!=0) {
count++;
System.out.println("---------------" + count
+ "分钟---------------");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
TimeCount timecount = new TimeCount();
Thread timecountThread = new Thread(timecount);
timecountThread.start();
}
}

我的更多文章:
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的计时器应用程序源代码: MainActivity.java: ```java public class MainActivity extends AppCompatActivity { private TextView timerTextView; private Button startButton, pauseButton, resetButton; private CountDownTimer countDownTimer; private boolean timerRunning; private long startTimeInMillis, timeLeftInMillis; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timerTextView = findViewById(R.id.timerTextView); startButton = findViewById(R.id.startButton); pauseButton = findViewById(R.id.pauseButton); resetButton = findViewById(R.id.resetButton); startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!timerRunning) { startTimer(); } } }); pauseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (timerRunning) { pauseTimer(); } } }); resetButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { resetTimer(); } }); } private void startTimer() { countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) { @Override public void onTick(long millisUntilFinished) { timeLeftInMillis = millisUntilFinished; updateTimer(); } @Override public void onFinish() { timerRunning = false; updateButtons(); } }.start(); timerRunning = true; updateButtons(); } private void pauseTimer() { countDownTimer.cancel(); timerRunning = false; updateButtons(); } private void resetTimer() { timeLeftInMillis = startTimeInMillis; updateTimer(); updateButtons(); } private void updateTimer() { int minutes = (int) (timeLeftInMillis / 1000) / 60; int seconds = (int) (timeLeftInMillis / 1000) % 60; String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds); timerTextView.setText(timeLeftFormatted); } private void updateButtons() { if (timerRunning) { startButton.setEnabled(false); pauseButton.setEnabled(true); resetButton.setEnabled(false); } else { startButton.setEnabled(true); pauseButton.setEnabled(false); resetButton.setEnabled(true); } } } ``` activity_main.xml: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.timer.MainActivity"> <TextView android:id="@+id/timerTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="60sp" android:text="00:00" android:layout_centerInParent="true"/> <Button android:id="@+id/startButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" android:layout_below="@+id/timerTextView" android:layout_marginTop="16dp" android:layout_centerHorizontal="true"/> <Button android:id="@+id/pauseButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pause" android:layout_toLeftOf="@+id/resetButton" android:layout_below="@+id/timerTextView" android:layout_marginTop="16dp" android:layout_marginRight="16dp" android:enabled="false"/> <Button android:id="@+id/resetButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Reset" android:layout_alignParentRight="true" android:layout_below="@+id/timerTextView" android:layout_marginTop="16dp" android:layout_marginLeft="16dp" android:enabled="false"/> </RelativeLayout> ``` 这个应用程序包含一个 `TextView` 显示剩余时间,以及三个按钮:`Start`,`Pause` 和 `Reset`。当用户点击 `Start` 按钮时,计时器开始倒计时,并且 `Start` 按钮禁用,`Pause` 按钮启用。当用户点击 `Pause` 按钮时,计时器暂停,并且 `Pause` 按钮禁用,`Start` 按钮启用。当用户点击 `Reset` 按钮时,计时器被重置为初始状态,即未开始状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值