java - Punch Clock While 循环故障
问题描述
我正在尝试用这个创建我自己的计时器,并想用这个计时器制作一个打卡钟,但我的循环没有重复(时间:0:0)它只在我输入 2 个数字后才会重复,我想不出为什么请帮助我以前的 Thread.sleep(60000) 低于分钟 = 分钟 + 1 所以我让它以分钟的速度工作但被删除了这样我可以更快地看到结果
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
int minute = 0, hour = 0, punchi = 0, puncho = 0, TP, TH = 0, i = 0, o = 0;
int power = 10;
boolean k = true;
String si, so;
while (k == true) {
System.out.println("Time: " + hour + ":" + minute);
k = true;
minute = minute + 30;
k = true;
if (minute == 60) {
hour = hour + 1;
minute = 0;
k = true;
}
else
k = true;
if (hour == 24) {
hour = 0;
k = true;
}
else
k = true;
si = sc.nextLine();
so = sc.nextLine();
if (si == "punchi") {
i = sc.nextInt();}
else
k = true;
i = sc.nextInt();
if (so == "puncho"){
o = sc.nextInt();
}
else
k = true;
if (o > 0) {
TH = (TH + o - i);
o = 0;
i = 0;
System.out.println("Power off?");
power = sc.nextInt();
k = true;}
else
k = true;
if (power == 9) {
TP = TH * 14;
k = false;
System.out.println("your total pay is " + TP); }
else
k = true;
}
}
}
}
解决方案
在您输入两个数字之前它不会循环的原因是因为您正在使用扫描仪的下一行函数,直到它接收到一个值,因为您有两个下一行调用,它需要两个值才能继续循环。
如果它没有阻塞,循环可能会发生得太快,以至于您无法输入句子,因为在循环迭代运行时,您可能只输入了一个字母。
如果你想使用这个函数,你需要多线程你的程序,这样while循环在主线程上运行,下一行函数运行第二个线程,当第二个收到它正在寻找的东西时,它通知第一个线程。
推荐阅读
- office-js - 如何使用。officejs中的Isempty
- c++ - &C::c 和 &(C::c) 有什么区别?
- excel - 如何找到为主变量的子变量编码 Y 或 N 的变量的计数?
- css - 带有svg的关键帧动画
- google-apps-script - 在 Marketplace 中找不到域范围编辑器插件
- angular - Angular :: 从嵌套插座链接到父“路由器插座”
- ansible - 更新 Ansible Galaxy 页面
- java - 如何使挥杆动画更快更流畅
- android - Badtokenexception androidx
- promise - dojo/promise/all 可以一起用于 xhr request 和 arcgis querytask 吗?