为什么Java';中断';声明不起作用?
这是我的密码:为什么Java';中断';声明不起作用?,java,loops,while-loop,Java,Loops,While Loop,这是我的密码: public static void main(String args[]) throws Exception { int length; Scanner input = new Scanner (System.in); Scanner scanner = new Scanner(System.in); System.out.println(" How many dates would you like to inc
public static void main(String args[]) throws Exception {
int length;
Scanner input = new Scanner (System.in);
Scanner scanner = new Scanner(System.in);
System.out.println(" How many dates would you like to include for the expiration dates notification? ");
length=scanner.nextInt();
String[] dates = new String[length];
System.out.println(" input date, formatting is hh:mm:ss");
for(int i=0; i<length; i++)
{
dates[i]=scanner.next();
}
System.out.println("== DATE LIST ==");
for (int i=0; i<length; i++)
{
System.out.println(dates[i]);
}
int loopcount;
System.out.print("Please enter how much notification you would like to put in: ");
loopcount = input.nextInt();
System.out.println("== EXPIRE LIST ==\n");
while (true) {
for (String z : dates) {
Thread.sleep(50);
LocalDateTime now = LocalDateTime.now();
Date convertednow = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
String strnow = dateFormat.format(convertednow);
if (strnow.equals(z)) {
System.out.print(z+" has expired\n");
int i = 0;
while(i < loopcount) {
System.out.println("Notification stopped");
i++;
if (i == 3) {
break;
}
}
}
}
}
}
publicstaticvoidmain(字符串args[])引发异常{
整数长度;
扫描仪输入=新扫描仪(System.in);
扫描仪=新的扫描仪(System.in);
System.out.println(“您希望在到期日期通知中包含多少日期?”);
长度=scanner.nextInt();
字符串[]日期=新字符串[长度];
System.out.println(“输入日期,格式为hh:mm:ss”);
对于(int i=0;i,break
语句退出最内部的循环,即while(i
循环,而不是中间for(String z:dates)
循环或外部while(true)
循环
如果希望中断
退出外部循环,可以使用标签:
System.out.println(“==过期列表==\n”);
MAIN:while(true){/语句退出最内部的循环,即while(i
循环,而不是中间for(String z:dates)
循环或外部while(true)
循环
如果希望中断
退出外部循环,可以使用标签:
System.out.println(“==过期列表==\n”);
MAIN:while(true){//您可以通过在外部while上放置一个标签
并在该标签上断开来实现。断开通常只适用于执行它的立即循环
。因此内部断开
仅断开内部while循环
另一个选项是在循环中使用setableboolean
,然后有条件地将它们设置为false,以控制它们何时停止
System.out.println("== EXPIRE LIST ==\n");
outer:
while (true) {
for (String z : dates) {
Thread.sleep(50);
LocalDateTime now = LocalDateTime.now();
Date convertednow =
Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
String strnow = dateFormat.format(convertednow);
if (strnow.equals(z)) {
System.out.print(z+" has expired\n");
int i = 0;
while(i < loopcount) {
System.out.println("Notification stopped");
i++;
if (i == 3) {
break outer;
}
}
}
}
System.out.println(“==过期列表==\n”);
外部:
虽然(正确){
对于(字符串z:日期){
睡眠(50);
LocalDateTime now=LocalDateTime.now();
日期convertednow=
Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
SimpleDataFormat dateFormat=新的SimpleDataFormat(“hh:mm:ss”);
字符串strnow=dateFormat.format(convertednow);
如果(strnow.equals(z)){
系统输出打印(z+“已过期\n”);
int i=0;
而(i
您可以在外部while上放置一个标签,然后在该标签上断开。断开通常只适用于执行它的立即循环
。因此内部断开
仅断开内部while循环
另一个选项是在循环中使用setableboolean
,然后有条件地将它们设置为false,以控制它们何时停止
System.out.println("== EXPIRE LIST ==\n");
outer:
while (true) {
for (String z : dates) {
Thread.sleep(50);
LocalDateTime now = LocalDateTime.now();
Date convertednow =
Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
String strnow = dateFormat.format(convertednow);
if (strnow.equals(z)) {
System.out.print(z+" has expired\n");
int i = 0;
while(i < loopcount) {
System.out.println("Notification stopped");
i++;
if (i == 3) {
break outer;
}
}
}
}
System.out.println(“==过期列表==\n”);
外部:
虽然(正确){
对于(字符串z:日期){
睡眠(50);
LocalDateTime now=LocalDateTime.now();
日期convertednow=
Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
SimpleDataFormat dateFormat=新的SimpleDataFormat(“hh:mm:ss”);
字符串strnow=dateFormat.format(convertednow);
如果(strnow.equals(z)){
系统输出打印(z+“已过期\n”);
int i=0;
而(i
break
只会从内部循环中断。break
只会离开最内部的循环。不要在while(true)
时使用,而是在(运行)时检查布尔值
在到达break
之前,将其设置为false
,或者您可以将其拆分为行标签。旁注:您可以创建一个日期实例,仅使用new Date()即可表示当前日期和时间
。如果您只需要一个日期对象,则根本不需要使用LocalDateTime、ZoneId或Instant。中断
只会中断您的内部循环。中断
只会离开最里面的循环。不要使用while(true)
而是在运行时检查布尔值
在到达break
之前将其设置为false
,或者您可以将其设置为行标签。旁注:您可以使用新建日期()
创建一个表示当前日期和时间的日期实例。如果您只需要一个日期对象,则根本不需要使用LocalDateTime、ZoneId或Instant。