一道关于JAVA的题目,急求答案。
(1)用继承Thread类方法创建线程MyThread类,该线程的功能是输出26个英文字母(大写,不换行),每输出一个字母后,休眠时间是500—2000毫秒的随机数;(2...
(1)用继承Thread类方法创建线程MyThread类,该线程的功能是输出26个英文字母(大写,不换行),每输出一个字母后,休眠时间是500—2000毫秒的随机数;
(2)用实现Runnable接口方法创建线程MyRunnalbe类,该线程的功能是输出26个英文字母(小写,不换行),每输出一个字母后,休眠时间是500—2000毫秒的随机数;
(3)在主类中分别创建这两个类的对象t1、t2,并启动。
(提示:500—2000的随机数表示:500+(2000-5000)*Math.random() )
附上我自己的答案:
class MyThread extends Thread{
public void run(){
char ch;
try{
for (ch='A';ch<='Z';ch++){
System.out.print(ch+" ");
sleep((int)(500+(2000-5000)*Math.random()));
}
}catch (InterruptedException e){
return;
}
}
}
class MyRunnable implements Runnable {
public void run(){
char ch;
try{
for (ch='a';ch<='z';ch++){
System.out.print(ch+" ");
Thread.sleep((int)(500+(2000-5000)*Math.random()));
}
}catch (InterruptedException e){
return;
}
}
}
public class ThreadTest {
public static void main(String args[]) {
MyThread t1 = new MyThread();
MyRunnable r = new MyRunnable();
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
} 请问为什么无法编译? 展开
(2)用实现Runnable接口方法创建线程MyRunnalbe类,该线程的功能是输出26个英文字母(小写,不换行),每输出一个字母后,休眠时间是500—2000毫秒的随机数;
(3)在主类中分别创建这两个类的对象t1、t2,并启动。
(提示:500—2000的随机数表示:500+(2000-5000)*Math.random() )
附上我自己的答案:
class MyThread extends Thread{
public void run(){
char ch;
try{
for (ch='A';ch<='Z';ch++){
System.out.print(ch+" ");
sleep((int)(500+(2000-5000)*Math.random()));
}
}catch (InterruptedException e){
return;
}
}
}
class MyRunnable implements Runnable {
public void run(){
char ch;
try{
for (ch='a';ch<='z';ch++){
System.out.print(ch+" ");
Thread.sleep((int)(500+(2000-5000)*Math.random()));
}
}catch (InterruptedException e){
return;
}
}
}
public class ThreadTest {
public static void main(String args[]) {
MyThread t1 = new MyThread();
MyRunnable r = new MyRunnable();
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
} 请问为什么无法编译? 展开
2个回答
展开全部
那是因为你随机数产生的是负数,你说让一个线程休眠时间为负数,那怎么可能,正确如下:class MyThread extends Thread{
public void run(){
char ch;
try{
for (ch='A';ch<='Z';ch++){
System.out.print(ch+" ");
Thread.sleep((int)(500+(2000-500)*Math.random()));
}
}catch (InterruptedException e){
return;
}
}
}
class MyRunnable implements Runnable {
public void run(){
char ch;
try{
for (ch='a';ch<='z';ch++){
System.out.print(ch+" ");
Thread.sleep((int)(500+(2000-500)*Math.random()));
}
}catch (InterruptedException e){
return;
}
}
}
public class ThreadTest {
public static void main(String args[]) {
MyThread t1 = new MyThread();
MyRunnable r = new MyRunnable();
Thread t2 = new Thread(r);
t1.run();
t2.start();
}
}
如果是t1.start()那么将会交替产生大小字母,而t1.run()则会产生出所有大写字母后,再差生所有小写字母
public void run(){
char ch;
try{
for (ch='A';ch<='Z';ch++){
System.out.print(ch+" ");
Thread.sleep((int)(500+(2000-500)*Math.random()));
}
}catch (InterruptedException e){
return;
}
}
}
class MyRunnable implements Runnable {
public void run(){
char ch;
try{
for (ch='a';ch<='z';ch++){
System.out.print(ch+" ");
Thread.sleep((int)(500+(2000-500)*Math.random()));
}
}catch (InterruptedException e){
return;
}
}
}
public class ThreadTest {
public static void main(String args[]) {
MyThread t1 = new MyThread();
MyRunnable r = new MyRunnable();
Thread t2 = new Thread(r);
t1.run();
t2.start();
}
}
如果是t1.start()那么将会交替产生大小字母,而t1.run()则会产生出所有大写字母后,再差生所有小写字母
展开全部
楼上说的对啊,不过该这样改,把500+(2000-5000)*Math.random() 改成500+15000*Math.random()
class MyThread extends Thread{
public void run(){
char ch;
try{
for (ch='A';ch<='Z';ch++){
System.out.print(ch+" ");
sleep((int)(500+15000*Math.random()));
}
}catch (InterruptedException e){
return;
}
}
}
class MyRunnable implements Runnable {
public void run(){
char ch;
try{
for (ch='a';ch<='z';ch++){
System.out.print(ch+" ");
Thread.sleep((int)(500+15000*Math.random()));
}
}catch (InterruptedException e){
return;
}
}
}
public class ThreadTest {
public static void main(String args[]) {
MyThread t1 = new MyThread();
MyRunnable r = new MyRunnable();
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
}
class MyThread extends Thread{
public void run(){
char ch;
try{
for (ch='A';ch<='Z';ch++){
System.out.print(ch+" ");
sleep((int)(500+15000*Math.random()));
}
}catch (InterruptedException e){
return;
}
}
}
class MyRunnable implements Runnable {
public void run(){
char ch;
try{
for (ch='a';ch<='z';ch++){
System.out.print(ch+" ");
Thread.sleep((int)(500+15000*Math.random()));
}
}catch (InterruptedException e){
return;
}
}
}
public class ThreadTest {
public static void main(String args[]) {
MyThread t1 = new MyThread();
MyRunnable r = new MyRunnable();
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询