【问题标题】:Time limit for an input输入的时间限制
【发布时间】:2011-08-16 18:35:52
【问题描述】:

假设我有一个代码要求用户提供一些输入,如下所示:

for (condition) {
System.out.println("Please give some input");
System.in.read();
} //lets say this loop repeats 3 times and i face a problem during second iteration

但我想给用户一个 60 秒的时间限制,然后抛出一个异常(在这种情况下,我认为是 TimeOutException)。我该怎么做?

【问题讨论】:

标签: java eclipse


【解决方案1】:
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
public class test
{
    private String str = "";

    TimerTask task = new TimerTask()
    {
        public void run()
        {
            if( str.equals("") )
            {
                System.out.println( "you input nothing. exit..." );
                System.exit( 0 );
            }
        }    
    };

    public void getInput() throws Exception
    {
        Timer timer = new Timer();
        timer.schedule( task, 10*1000 );

        System.out.println( "Input a string within 10 seconds: " );
        BufferedReader in = new BufferedReader(
        new InputStreamReader( System.in ) );
        str = in.readLine();

        timer.cancel();
        System.out.println( "you have entered: "+ str ); 
    }

    public static void main( String[] args )
    {
        try
        {
            (new test()).getInput();
        }
        catch( Exception e )
        {
            System.out.println( e );
        }
        System.out.println( "main exit..." );
    }
}

【讨论】:

  • 还有其他更简单的方法吗?因为我的 java.util 包中没有 timer 和 timertask 类
  • 启动线程,只需使用 while 循环为您的时间和间隔后使用 thread.destroy();
  • 我有话要问你..让我说我写了这个 - Thread t = new Thread(); t.start(); //我启动了一个线程做{//我的逻辑}while(condition); t.destroy(); //我破坏了线程。但不知道为什么。我的代码骨架正确吗?我必须在while()中放入什么条件?你能解开我所有的疑惑吗?
  • 在您的情况下,您不需要使用 while 因为您想在用户输入某些内容时退出,所以如果用户没有输入某些内容,那么您必须等待指定的时间,所以您的登录是正确的,只需删除 while循环
  • 我想我没有正确地告诉你。我想等待用户输入一些值。但我只想等几秒钟。在那个指定的时间之后,我想显示一个错误并退出。现在我的问题是,sysout 和 sysinread - 都在一个 for 循环中。那么我不可能继续下一次迭代了吗?我已经更新了我的代码..
【解决方案2】:

我使用 joda-time 来处理这类事情:

行家:

  <!--  Joda Time -->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>1.6.2</version>
    </dependency>

提示输入时,设置LocalDateTime变量:

 LocalDateTime timeOut = new LocalDateTime().plusSeconds(15);

然后循环直到用户输入或达到超时:

 if (timeOut.isBefore(new LocalDateTime())) {
 //throw your exception if this case happens
 }

在投反对票之前:这只是一个快速的过程:p

干杯

【讨论】:

  • 还有其他更简单的方法吗?因为我没有在我的项目中使用那个maven。
  • 你也可以在没有 maven 的情况下使用 joda-time :p
【解决方案3】:

像这样简单的事情怎么样:

    Scanner reader = new Scanner(System.in); 
    System.out.println("Enter a number: ");
    long limit = 5000L;
    long startTime = System.currentTimeMillis();
    Long l = reader.nextLong();
    if ((startTime + limit) < System.currentTimeMillis())
        System.out.println("Sorry, your answer is too late");
    else
        System.out.println("Your answer is on time");

这不会抛出异常,只会通知用户他的回答为时已晚。 (与本文提到的另一个问题有关)。

【讨论】:

  • 这将等到您提供输入。
猜你喜欢
  • 2015-12-06
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 2013-03-09
相关资源
最近更新 更多