题目描述:
程序改错题。请修改下列代码,使程序能够输出正确的结果。
import java.util.Scanner; public class Main { public static void main(String[] args) { Thread t = new Thread(new RunHandler()); t.run(); } } class RunHandler { public void run() { Scanner in = new Scanner(System.in); int x = in.nextInt(); System.out.println("run"); } }
输入格式:
输入一个整数x。
输出格式:
输出x行,每行一个字符串“run”。
输入样例:
4
输出样例:
run
run
run
run
最终代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Thread t = new Thread(new RunHandler());
t.start();
}
}
class RunHandler extends Thread {
public void run() {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
for (int i = 0; i < x; i++) {
System.out.println("run");
}
}
}
总结:复习线程最基本的继承Thread类。