Java 用户输入的时间限制
我在为自己制作一个小游戏时遇到了一些问题。我遇到的问题是用户输入超时。例如:游戏将在屏幕上显示一个字母。用户必须在一定时间内输入该字母;如果他们不这样做,他们就会受到伤害。如果有人能解释我可以用什么来解决这个问题,那就太好了Java 用户输入的时间限制,java,Java,我在为自己制作一个小游戏时遇到了一些问题。我遇到的问题是用户输入超时。例如:游戏将在屏幕上显示一个字母。用户必须在一定时间内输入该字母;如果他们不这样做,他们就会受到伤害。如果有人能解释我可以用什么来解决这个问题,那就太好了 package weapons; import java.util.Random; import java.util.Scanner; class wepons { public static void main(String args[]){
package weapons;
import java.util.Random;
import java.util.Scanner;
class wepons {
public static void main(String args[]){
int weapondamage =0 , health = 0 , hits = 0, potion = 0, beast = 0, beastdmg = 0, letter = 0;
String weapon;
;
Scanner in = new Scanner (System.in);
System.out.println ("please pick a wepon that will kill the boss \n");
weapon = in.nextLine().toLowerCase();
if (weapon.equals("sword")){
System.out.println("you have picked the sword");
weapondamage = (int) ((Math.random()*100));
health = weapondamage*3;
}
System.out.println("Sword damage is " + weapondamage);
System.out.println("Boss health is " + health);
System.out.println("Enter the amount of hits it will take to kill the boss.");
hits = in.nextInt();
if(hits == health/weapondamage) {
System.out.println("you have killed the boss.The boss had droped a potion.\nYou pick up the potion and drink it!\nYour health has now gone up to 350hp");
}
else {
System.out.print("You have failed to kill the boss");
}
potion = (int)350;
beastdmg = (int) ((Math.random()*60));
System.out.println("By killing the boss you have awoken the beast!");
System.out.println("The beast nocks you onto your back");
System.out.println("The beast will hit you for"+beastdmg+"damage");
System.out.println("You can block the beast damage by hitting the write key");
Random r = new Random();
int c = r.nextInt(26) + (byte)'a';
System.out.println((char)c);
letter = in.next().charAt(0);
if( letter == ((char)c)){
System.out.println("You blocked the beast damage");
}
else {
System.out.print("You took damage");
}
}
} // main
有多种方法可以做到这一点,我用OP的风格,以最简单的方式回答,所以测量用户按下字母的时间。下面是一个功能片段:
System.out.println("You can block the beast damage by hitting the write key");
long startTime = System.currentTimeMillis();
Random r = new Random();
int c = r.nextInt(26) + (byte) 'a';
System.out.println((char) c);
char letter = in.next().charAt(0);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
if (letter == ((char) c) && elapsedTime <=5000) {
System.out.println("You blocked the beast damage");
} else {
System.out.print("You took damage");
}
System.out.println(“你可以通过按写键来阻止野兽的伤害”);
long startTime=System.currentTimeMillis();
随机r=新随机();
int c=r.nextInt(26)+(字节)'a';
System.out.println((char)c);
字符字母=in.next().charAt(0);
长停止时间=System.currentTimeMillis();
长延时=停止时间-开始时间;
如果(letter==((char)c)&&elapsedTime您需要查看线程
使用线程,您可以一次执行多个进程,而不仅仅是逐行线性编码
例如,你可以说:
while(health > 0){
Thread.sleep(5000); //parameter for sleep is milliseconds, so this is 5 seconds
if(weapon == null){
health -= damage;
}else{ break; }
}
问题是,您可能需要创建一个单独的类来扩展线程以调用损坏损失…如果时间方面是必要的,我建议您阅读线程。不要把这段代码从字面上剪下来,这只是一个基本的大纲…根据我对线程的经验,您需要创建一个d返回武器值,因为您希望线程检查用户是否输入了武器值
您可能还需要参考本线程教程:
“有问题”并没有告诉我们问题是什么……这段代码是如何不起作用的?作为询问者,删除不必要的代码会对您有利。只包括与您的问题相关的代码(因此您预测的代码需要时间限制)如有必要,请用文字解释任何其他代码。请参阅,您可能希望为此创建一个单独的类,该类扩展Thread以调用损坏损失
什么?为什么?永远不要从Thread
扩展,永远不要。