How would I loop my program 5 seconds after the answer is given
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(inpu开发者_开发技巧t).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;
}
}
}
}
You can always try something like this to wait for five seconds
try { Thread.sleep(5000L); } catch(InterruptedException ex) {}
don't know if I understood, but you should try something like : Thread.sleep(5000) if you want your program to sleep for 5 seconds.
Use Thread.sleep(5000L) to make your program sleep for 5 seconds.
精彩评论