Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 实战错误讨论  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 运行这个程序出问题,为什么啊??(ArrayIndexOutOfBoundsException)
ybyt530





发贴: 4
积分: 0
于 2005-11-06 12:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
下面是源代码,<JAVA核心技术>一卷上的源代码,我绝对没写错!

import java.util.*;
import javax.swing.*;

public class LotteryDrawing
{
public static void main(String[] args)
{
String input=JOptionPane.showInputDialog
("How many numbers do you need to draw?");
int k=Integer.parseInt(input);

input=JOptionPane.showInputDialog
("What is the highest number you can draw");
int n=Integer.parseInt(input);

//file an array with numbers 1 2 3 4...n
int[] numbers=new int[n];
for(int i=0;i<numbers.length;i++)
numbers[i]=i+1;

//draw k numbers and put them into a second array
int[] result=new int[k];
for(int i=0;i<result.length;i++)
{
//make a randm index between 0 and n-1
int r=(int)(Math.random()*n);

//pick them element at the random location
result[i]=numbers[r];

//move the last element into the random location
numbers[r]=numbers[n-1];
n--;
}

//print the sorted array
Arrays.sort(result);
System.out.println
("Bet the following combination.It'll make you rich!");
for(int i=0;i<result.length;i++)
System.out.println(result[i]);
System.exit(0);

}
}

编译时没问题
运行时出现问题:

java.lang.ArrayIndexOutOfBoundsException: -1
  at LotteryDrawing.main(LotteryDrawing.java:44)

这是怎么回是?
这样的问题我看不懂啊?只好劳烦大家帮看看了,浪费大家点时间,这种运行问题我怎么才能知道是哪里出问题呢?看不懂怎么办.我想以后能自立点这样的问题不到处问了,呵呵.如果有办法的话告诉我哦.小弟谢谢了!


why edited on 2005-11-06 18:53

毅 语心开惊天变
薄 情无忌空自言
雲 海纵深遥相望
天 定缘属吾心间

话题树型展开
人气 标题 作者 字数 发贴时间
6827 运行这个程序出问题,为什么啊??(ArrayIndexOutOfBoundsException) ybyt530 1664 2005-11-06 12:10
4855 Re:运行这个程序出问题,为什么啊??(ArrayIndexOutOfBoundsException) why 97 2005-11-06 19:24
5471 Re:运行这个程序出问题,为什么啊??(ArrayIndexOutOfBoundsException) phoenixdx 263 2005-11-06 21:48

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923