简体   繁体   English

如何制作一个无限的java程序?

[英]How to make an infinite java program?

I want to make a utility program which shows the date, hour ... :我想制作一个显示日期、小时...的实用程序:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class hh {
    public static void main(String[] args) {
        Scanner mihai = new Scanner (System.in);
        Date dNow = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("dd.MM.yyyy");
        Date hNow = new Date( );
        SimpleDateFormat ht = new SimpleDateFormat ("kk:mm");
        String lol;
        lol = mihai.nextLine();
        switch (lol) {
            case "Date":  
                lol = ft.format(dNow);
                break;
            case "Hour":  
                lol = ht.format(hNow);
                break;
            case "?": 
                lol = "2. Hour";
                System.out.println("Supported functions:");
                System.out.println("1. Date");
                break;
            default: 
                lol = "Type ? for help";
                break;
        }
    }
}

But I use eclipse neon for editing the code, but after 1 use the program is terminated.但我使用 eclipse neon 来编辑代码,但在使用 1 次后程序终止。 I want to: When the program is at final auto restart it.(go to line 8).我想:当程序在最后自动重新启动它。(转到第 8 行)。

To repeat something you can use a while-loop.要重复某些内容,您可以使用 while 循环。

The easiest method to perfom this, is to put your code between while(true).执行此操作的最简单方法是将代码放在 while(true) 之间。 The while loop is looping the code as long as the condition between the brackets is true.只要括号之间的条件为真,while 循环就会循环代码。 If you write while(true) the condition is always true.如果你写 while(true) 条件总是为真。 That is why it is always repeating your code immediately.这就是为什么它总是立即重复您的代码。

while(true){
    //your code
}

More information at: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html更多信息请访问: https : //docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM