两道java题目

vipstepstep 2011-06-09 09:09:56
import java.io.*;

public class Reverse
{
public static void main(String args[])
{
int i, n = 10;
int a[] = new int [10];

for ( i = 0; i < n; ++i ) {
try {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
a[i] = Integer.parseInt(br.readLine());

} catch ( IOException e ){};
}

for ( i = n - 1; i >= 0; --i) {
System.out.print(a[i]+" ");
}
System.out.println();

}
}


这段代码编译没有问题,但是运行的时候出了错误?不知道哪里错了?

创建两个Thread类对象,第一个对象的字符串为“XX学院”,暂停时间 为50毫秒,第二个对象的字符串为“母校明天更美好”,暂停时间为100毫秒,分别激活这两个线程,每个线程被执行20次。利用实现Runnable接口创建线程。

汗,还没看到线程,要写线程的题目,不会写啊。
...全文
138 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fskjb01 2011-06-09
  • 打赏
  • 举报
回复
Integer.parseInt。。。你输入的是数字么
周靖峰 2011-06-09
  • 打赏
  • 举报
回复
第二题:
public class Test
{
public static void main(String[] args)
{
new Thread(new FirstRunnable()).start();
new Thread(new SecondRunnable()).start();
}
}

class FirstRunnable implements Runnable
{
public void run()
{
for (int i = 0; i < 20; i++)
{
System.out.println("XX学院");
try
{
Thread.sleep(50);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}

class SecondRunnable implements Runnable
{
public void run()
{
for (int i = 0; i < 20; i++)
{
System.out.println("母校明天会更好");
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
happysmhzp 2011-06-09
  • 打赏
  • 举报
回复
没问题啊
周靖峰 2011-06-09
  • 打赏
  • 举报
回复
第一题,把BufferedReader br = new BufferedReader(new InputStreamReader(System.in));放for外面去

import java.io.*;

public class Reverse
{
public static void main(String args[])
{
int i, n = 10;
int a[] = new int [10];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

for ( i = 0; i < n; ++i )
{
try
{
a[i] = Integer.parseInt(br.readLine());
}
catch ( IOException e )
{};
}

for ( i = n - 1; i >= 0; --i)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
}
gentleboy2009 2011-06-09
  • 打赏
  • 举报
回复
首先上面代码运行了下 没问题,反序输出。 下面线程编程其实很简单,找个线程的例子,照猫画虎

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧