PTA_2022实验七
判断+填空
7-1 创建一个倒数计数线程
创建一个倒数计数线程。要求:1.该线程使用实现Runnable接口的写法;2.程序该线程每隔0.5秒打印输出一次倒数数值(数值为上一次数值减1)。
输入格式:
N(键盘输入一个整数)
输出格式:
每隔0.5秒打印输出一次剩余数
输入样例:
6
输出样例:
在这里给出相应的输出。例如:
6
5
4
3
2
1
0
import java.util.Scanner;
class Mythread implements Runnable
{
public static int num;
public Mythread(int n)
{
this.num=n;
}
public void run()
{
for(int i=num;i>=0;i--)
{
try
{
Thread.sleep(500);
}
catch (Exception e)
{
// TODO: handle exception
}
System.out.println(i);
}
}
}
public class Main
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
Mythread m=new Mythread(n);
Thread t=new Thread(m);
t.start();
}
}