题目1请使用匿名Runnable子类的方式实现线程线程中计算1--500所有数字的累加和并打印计算结果题目2请使用继承Thread类的方式定义一个线程类在run方法中循环10次每1秒循环1次每次循环按yyyyMMdd HHmmss的格式打印当前系统时间。请定义测试类并定义main方法启动此线程观察控制台打印。题目3请使用代码描述1 写一个方法实现用户登陆传入用户名和密码2 如果用户名错误就抛出
题目1
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
int sum = 0;
for (int i = 1; i <= 500; i++) {
sum += i;
}
System.out.println("1--500所有数字的累加和为:" + sum);
}
});
thread.start();
}
}
题目2
public class MyThread extends Thread {
@Override
public void run() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (int i = 1; i <= 10; i++) {
System.out.println(dateFormat.format(new Date()));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
题目3
public class LoginException extends Exception {
public LoginException(String message) {
super(message);
}
}
public class Main {
public static void main(String[] args) {
try {
login("admin", "123");
} catch (LoginException e) {
e.printStackTrace();
}
}
public static void login(String username, String password) throws LoginException {
if (!"admin".equals(username)) {
throw new LoginException("用户名不存在");
}
if (!"admin".equals(password)) {
throw new LoginException("密码错误");
}
System.out.println("欢迎" + username);
}
}
``
原文地址: http://www.cveoy.top/t/topic/fojS 著作权归作者所有。请勿转载和采集!