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