Java 我的计时器怎么了?
好的,我已经完成了这个计时器脚本的一半,但是我注意到当它第二次运行函数时,它不再接受输入了,知道为什么吗? 它接收所有数据,并在启动时等待输入Java 我的计时器怎么了?,java,netbeans,timer,Java,Netbeans,Timer,好的,我已经完成了这个计时器脚本的一半,但是我注意到当它第二次运行函数时,它不再接受输入了,知道为什么吗? 它接收所有数据,并在启动时等待输入 public class ManagingLifts { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Timer timer = new Timer(); DeliveryMan
public class ManagingLifts {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Timer timer = new Timer();
DeliveryMan d = new DeliveryMan();
Hacker h = new Hacker();
Lift id = new Lift();
ArrayList<Person> p = new ArrayList();
ArrayList<String> who = new ArrayList();
int MINUTES = 2; // setting the duration of timer for people entering lift
timer.scheduleAtFixedRate(new TimerTask() // task to be run every 2 minute
{
@Override
public void run()
{
int counter = 0;
int HorD = (int)(Math.random() * 2) + 1; // decides who gets into the lift
if (HorD <= 1) //
{
System.out.println("What is your name?");
String name = sc.nextLine();
System.out.println("What is your NRIC?");
String nric = sc.nextLine();
System.out.println("What is your Country?");
String country = sc.nextLine();
System.out.println("What floor do you wan to go?");
int floor = sc.nextInt();
if(floor >=50) //making sure they only go to allowed floors
{
System.out.println("You can only access floors 40 - 50!");
}
Person list = new Person(name, nric);
p.add(list);
h.setCountry(country);
who.add("Hacker"); // to have a list of who entered the lift
counter = +2;
}
else if (HorD >= 2)
{
System.out.println("What is your name?");
String name2 = sc.nextLine();
System.out.println("What is your NRIC?");
String nric2 = sc.nextLine();
System.out.println("What type of food are you delivering?");
String food = sc.nextLine();
System.out.println("What floor do you want to go?");
int floor2 = sc.nextInt();
if(floor2 >=31)
{
System.out.println("You can only access floors 2 - 30!");
}
Person list2 = new Person(name2, nric2);
p.add(list2);
d.setTypeofF(food);
who.add("DeliveryGuy"); // who entered the lift
counter = +2;
}
if(counter > 200) // stop running lift after 200min
{
timer.cancel();
}
}
}, 0, 1000 * 60 * MINUTES); // will run this function every 2min
}
}
公共类管理电梯{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
定时器=新定时器();
送货员d=新送货员();
黑客h=新黑客();
提升id=新提升();
ArrayList p=新的ArrayList();
ArrayList who=新的ArrayList();
int MINUTES=2;//设置进入电梯的计时器的持续时间
timer.scheduleAtFixedRate(new TimerTask()//每2分钟运行一次的任务
{
@凌驾
公开募捐
{
int计数器=0;
int HorD=(int)(Math.random()*2)+1;//决定谁进入电梯
if(HorD=50)//确保他们只去允许的楼层
{
System.out.println(“您只能访问40-50层!”);
}
人员列表=新人员(姓名,nric);
p、 添加(列表);
h、 国家(国家),;
who.add(“黑客”);//获取进入电梯的人员列表
计数器=+2;
}
否则如果(HorD>=2)
{
System.out.println(“你叫什么名字?”);
字符串名称2=sc.nextLine();
System.out.println(“你的NRIC是什么?”);
字符串nric2=sc.nextLine();
System.out.println(“你送的是什么类型的食物?”);
String food=sc.nextLine();
System.out.println(“您想去哪一层?”);
int floor2=sc.nextInt();
如果(楼层2>=31)
{
System.out.println(“您只能访问2-30层!”);
}
人员列表2=新人员(姓名2,nric2);
p、 添加(列表2);
d、 抵销(食物);
who.add(“送货员”);//谁进了电梯
计数器=+2;
}
如果(计数器>200)//200分钟后停止运行升降机
{
timer.cancel();
}
}
},0,1000*60*分钟);//将每2分钟运行一次此函数
}
}
读取楼层编号时,您只读取下一个整数,而不是输入
移动到下一行。由于未读取,因此下次循环开始时,名称将是空字符串
因此,在阅读地板时添加一个sc.nextLine()
,或者只读取一个字符串,然后自己使用Integer.parseInt
将其转换为整数
或者,您也可以通过重新创建扫描仪
,放弃系统中的所有输入。不要在任务外部创建扫描器,只需在任务运行时首先创建扫描器(因此在运行
方法内部)。读取楼层编号时,只读取下一个整数,而不是输入
移动到下一行。由于未读取,因此下次循环开始时,名称将是空字符串
因此,在阅读地板时添加一个sc.nextLine()
,或者只读取一个字符串,然后自己使用Integer.parseInt
将其转换为整数
或者,您也可以通过重新创建扫描仪
,放弃系统中的所有输入。与其在任务外部创建扫描仪,不如在任务运行时首先创建扫描仪(因此在run
方法内部)。Haha woah感谢快速响应是的,解决了问题,非常感谢!我没有注意到的楼层面积太大了哈哈哇谢谢你的快速反应是的,解决了问题非常感谢!还有我没有注意到的建筑面积xD