我想在我的游戏中添加计时器功能,你能告诉我如何在程序中添加15秒计时器吗?时间结束后,我希望输出显示为Try Again或者其他东西。代码如下:
import java.util.Scanner;
public class Main {
public static void main(String [] args)
{
rules();
Scanner s = new Scanner(System.in);
boolean[] cantCut = new boolean[6];
/* 0 - white
1 - red
2 - purple
3 - black
4 - green
5 - orange */
boolean exploded = false;
for(int i = 0; i <= 5 ; i++)
{
String input;
input = s.nextLine();
switch(input)
{
case "white":
if(cantCut[0])
exploded = true;
else
{
cantCut[0] = true; //white
cantCut[1] = true; //red
cantCut[2] = false; //purple
cantCut[3] = false; //black
cantCut[4] = false; //green
cantCut[5] = false; //orange
}
break;
case "red":
if(cantCut[1])
exploded = true;
else
{
cantCut[0] = true; //white
cantCut[1] = false; //red
cantCut[2] = false; //purple
cantCut[3] = false; //black
cantCut[4] = true; //green
cantCut[5] = true; //orange
}
break;
case "purple":
if(cantCut[2])
exploded = true;
else
{
cantCut[0] = true; //white
cantCut[1] = false; //red
cantCut[2] = true; //purple
cantCut[3] = false; //black
cantCut[4] = true; //green
cantCut[5] = true; //orange
}
break;
case "black":
if(cantCut[3])
exploded = true;
else
{
cantCut[0] = true; //white
cantCut[1] = true; //red
cantCut[2] = true; //purple
cantCut[3] = true; //black
cantCut[4] = false; //green
cantCut[5] = true; //orange
}
break;
case "green":
if(cantCut[4])
exploded = true;
else
{
cantCut[0] = false; //white
cantCut[1] = true; //red
cantCut[2] = true; //purple
cantCut[3] = true; //red
cantCut[4] = true; //black
cantCut[5] = false; //orange
}
break;
case "orange":
if(cantCut[5])
exploded = true;
else
{
cantCut[0] = true; //white
cantCut[1] = false; //red
cantCut[2] = true; //purple
cantCut[3] = false; //black
cantCut[4] = true; //green
cantCut[5] = true; //orange
}
break;
default:
System.out.println("Please enter a correct choice");
}
}
if(exploded)
System.out.println("BOOM!, Terrorists Win");
else
System.out.println("Bomb Defused, Counter-Terrorists Win.");
s.close();
}
public static void rules() {
System.out.println("If you cut a white cable you can't cut white or red cable.");
System.out.println("If you cut a red cable it is not allowed to cut a white, green or orange one.");
System.out.println("If you cut a purple cable you can't cut a purple, green, orange or white cable");
System.out.println("If you cut a black cable you have to cut a green one.");
System.out.println("If you cut a green one you have to cut an orange or white one.");
System.out.println("If you cut an orange cable you should cut a red or black one.");
System.out.println("Enter the 6 wires to be cut in the correct order to defuse the bomb:");
}
}