excuse me

a19861028 2007-09-14 09:38:49
static Object[] get()
{
Object[] list = new Object[10];
int i=0;
System.out.println("please input parame");
do
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader( isr );
try {

list[i] = br.readLine();
} catch (IOException e) {

e.printStackTrace();
}

}while(list[i]!="#");

for(i = 0; i<10; i++)
System.out.println("list["+i+"]="+list[i]);

return list;



}
请问有什么问题啊?运行不成功.
...全文
99 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qybao 2007-09-15
  • 打赏
  • 举报
回复
for( ;i<=n;i++) -> for(; i<n; i++) //数组下标是从0到n-1,当i=n时数组越界了

feathergirl 2007-09-15
  • 打赏
  • 举报
回复
n错了.
zephyr_cc 2007-09-14
  • 打赏
  • 举报
回复
把ArrayIndexOutOfBoundsException这个catch了直接break出for
a19861028 2007-09-14
  • 打赏
  • 举报
回复
public static void main(String[] args) {
Object a[]=null;
a=get();
Perm(a,0,2);

}
static Object[] get()
{
System.out.println("please input num");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader( isr );

int n=0;
try {
n = Integer.parseInt(br.readLine());
} catch (NumberFormatException e1) {

e1.printStackTrace();
} catch (IOException e1) {

e1.printStackTrace();
}



Object[] list = new Object[n];
int i=0;
System.out.println("please input parame");
for( ;i<=n;i++)
{
isr = new InputStreamReader(System.in);
br = new BufferedReader( isr );
try {

list[i] = br.readLine();
} catch (IOException e) {

e.printStackTrace();
}

}

for(i = 0; i<list.length; i++)
System.out.println("list["+i+"]="+list[i]);

return list;



}
就有Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
错误.
zephyr_cc 2007-09-14
  • 打赏
  • 举报
回复
主要是while (!list[i++].equals("#"));这里

static Object[] get() {
Object[] list = new Object[10];
int i = 0;
System.out.println("please input parame");
do {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {

list[i] = br.readLine();
} catch (IOException e) {
e.printStackTrace();
} catch (ArrayIndexOutOfBoundsException e) {
break;
}
} while (!list[i++].equals("#"));

for (i = 0; i < 10; i++)
System.out.println("list[" + i + "]=" + list[i]);
return list;
}

62,629

社区成员

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

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