在给出答案后5秒钟,我将如何循环我的程序

时间:2011-02-25 09:53:35

标签: java

import java.io.*;    

public class Printer   {
   public static void main (String[] args) throws Exception  {
   while(true){
    System.out.println("Enter a number:");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input = br.readLine(); 
    double number = Double.valueOf(input).doubleValue();
    System.out.println("Enter second number:");
    input = br.readLine();
    double number1= Double.valueOf(input).doubleValue();


    System.out.println("Enter operator(+, -, *, /): ");
    input = br.readLine();    //"+" "-" "*" "/"
    if (input.compareTo ("+") == 0)
    {
       System.out.println("Answer is: " + (number + number1));
       break;
    }
    else if (input.compareTo ("-") == 0)
    {
       System.out.println("Answer is: " + (number - number1));
       break;
      }
    else if (input.compareTo ("*") == 0)
    {
       System.out.println("Answer is: " + (number * number1));
       break;
    }
    else if (input.compareTo ("/") == 0)
    {
       System.out.println("Answer is: " + (number / number1));
       break;
    }

   }
 }
}

3 个答案:

答案 0 :(得分:2)

你总是可以尝试这样的事情等待五秒

try { Thread.sleep(5000L); } catch(InterruptedException ex) {}

答案 1 :(得分:1)

不知道我是否理解,但你应该尝试类似:Thread.sleep(5000),如果你想让你的程序睡5秒钟。

答案 2 :(得分:1)

使用Thread.sleep(5000L)让程序睡眠5秒钟。