虽然真正的循环只执行了有限的次数

时间:2018-03-23 20:02:37

标签: while-loop telegram

所以我有这个电报机器人,我在主弦args中有一个while循环,因为我需要它来检查永恒的东西。但过了一会儿甚至是emediatly,while循环停止了。

import org.telegram.telegrambots.ApiContextInitializer;

import org.telegram.telegrambots.TelegramBotsApi; import org.telegram.telegrambots.exceptions.TelegramApiException;

import java.time.Duration; import java.time.LocalDateTime;

public class Main {

public static void main(String[] args) {
    // Initialize Api Context
    ApiContextInitializer.init();
    boolean running = false;

    // Instantiate Telegram Bots API
    TelegramBotsApi botsApi = new TelegramBotsApi();
    SwtPremiumBot bot = new SwtPremiumBot();

    // Register our bot
    try {
        botsApi.registerBot(bot);
        System.out.println("BOT REGISTERED");
        running=true;
        //storing the filIDS
        String[] files=new String[9];
        files[0]="AgADBAADa6wxG1gRcVGzXAwfpuZjo0JZJhoABOvVf8G55uyhXeUEAAEC";
        files[1]="AgADBAADbKwxG1gRcVGGoZcrmZ786ph7jBoABG7SjlCeIwnjXngDAAEC";
        files[2]="AgADBAADbawxG1gRcVFwF6pUv1RskZ1GKRoABMefcdSzWLaxYWIFAAEC";
        files[3]="AgADBAADbqwxG1gRcVGB0noIZtEpgypbNBoABL1GJAMM9QxqElABAAEC";
        files[4]="AgADBAADcqwxG1gRcVGBiPqbusQgZmBLkRoABE-nBSP6FJZYGnEAAgI";
        files[5]="AgADBAADc6wxG1gRcVHwFx4ec9kWnafJKhoABMBQxr6mC9qHXFsFAAEC";
        files[6]="AgADBAADdKwxG1gRcVERF469BLWkMB0LkRkABOfM_ioz6u6iG0oAAgI";
        files[7]="AgADBAADdawxG1gRcVFa7fItTXuxtu0JjhoABMl076IfG9VFB3oDAAEC";
        files[8]="AgADBAADdqwxG1gRcVHEf5OseR1P4aTlkBkABFGmx73rlPOjUksAAgI";
        String[] gifs=new String[13];
        gifs[0]="CgADBAADYAIAAlgRcVFuikWviGM3ugI";
        gifs[1]="CgADBAADYQIAAlgRcVEEWh_ZQxpfNAI";
        gifs[2]="CgADBAADYgIAAlgRcVEpmx9MY-rSHwI";
        gifs[3]="CgADBAADYwIAAlgRcVF0pJYhUNuCIwI";
        gifs[4]="CgADBAADZAIAAlgRcVFvJlndVDLAaAI";
        gifs[5]="CgADBAADZQIAAlgRcVFzc-BYUtICfAI";
        gifs[6]="CgADBAADZgIAAlgRcVFML1TuOWgTFQI";
        gifs[7]="CgADBAADZwIAAlgRcVGDlLtcdA1gGgI";
        gifs[8]="CgADBAADaAIAAlgRcVHk0_p_0xy4XQI";
        gifs[9]="CgADBAADaQIAAlgRcVH9Y38kRM025QI";
        gifs[10]="CgADBAADagIAAlgRcVH7ZXg3Vvju9AI";
        gifs[11]="CgADBAADawIAAlgRcVG00SeL2VBKowI";
        gifs[12]="CgADBAADbAIAAlgRcVFetih9aTXW8gI";

        bot.setFileIds(files);
        bot.setGifs(gifs);

    } catch (TelegramApiException e) {
        e.printStackTrace();
        String error = e.toString();
        bot.addErrors(error, LocalDateTime.now());
    }

    LocalDateTime lastCheck = LocalDateTime.now();
    while (running){
        LocalDateTime now = LocalDateTime.now();
        Duration d = Duration.between(lastCheck, now);
        long duration = d.toMillis();

        if (duration>20000) {
            bot.checkCompleteOrders();
            bot.checkPublishTime();
            lastCheck = LocalDateTime.now();
            System.out.println("Orders checked.");
        }
    }
    String error = "WHILE LOOP BROKEN";
    bot.addErrors(error, LocalDateTime.now());
}

}

提前致谢。

0 个答案:

没有答案