为什么提示java.lang.ArrayIndexOutOfBoundsException: -1 ???
用随机数产生彩票序列的小程序,为什么提示数组下标的问题呢???importjava.util.*;publicclassLotteryDrawing{publicstat...
用随机数产生彩票序列的小程序,为什么提示数组下标的问题呢???
import java.util.*;
public class LotteryDrawing {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("要选几个数?");
int select_numbers = in.nextInt();
System.out.print("最大的数是多少?");
int all_numbers = in.nextInt();
int[] All_numbers = new int[all_numbers];
for(int i = 0; i < All_numbers.length; i++)
All_numbers[i] = i + 1;
int[] GuessResult = new int[select_numbers];
for(int j = 0; j < GuessResult.length; j++){
int r = (int)(Math.random() * all_numbers);
GuessResult[j] = All_numbers[r];
All_numbers[r] = All_numbers[all_numbers - 1];//此处报错
all_numbers--;
}
Arrays.sort(GuessResult);
System.out.println("我猜下面这几个能中~~~");
for(int r : GuessResult)
System.out.println(r);
}
} 展开
import java.util.*;
public class LotteryDrawing {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("要选几个数?");
int select_numbers = in.nextInt();
System.out.print("最大的数是多少?");
int all_numbers = in.nextInt();
int[] All_numbers = new int[all_numbers];
for(int i = 0; i < All_numbers.length; i++)
All_numbers[i] = i + 1;
int[] GuessResult = new int[select_numbers];
for(int j = 0; j < GuessResult.length; j++){
int r = (int)(Math.random() * all_numbers);
GuessResult[j] = All_numbers[r];
All_numbers[r] = All_numbers[all_numbers - 1];//此处报错
all_numbers--;
}
Arrays.sort(GuessResult);
System.out.println("我猜下面这几个能中~~~");
for(int r : GuessResult)
System.out.println(r);
}
} 展开
展开全部
for(int j = 0; j < GuessResult.length; j++){
int r = (int)(Math.random() * all_numbers);
GuessResult[j] = All_numbers[r];
All_numbers[r] = All_numbers[all_numbers - 1];//此处报错 当输入"最大的数"小于"要选的几个数"的时候,该for循环了GuessResult.length次,all_numbers-- 自减了GuessResult.length次,all_numbers-1,就会小于0了,All_numbers[all_numbers - 1]就会报ArrayIndexOutOfBoundsException啦
all_numbers--;
}
int r = (int)(Math.random() * all_numbers);
GuessResult[j] = All_numbers[r];
All_numbers[r] = All_numbers[all_numbers - 1];//此处报错 当输入"最大的数"小于"要选的几个数"的时候,该for循环了GuessResult.length次,all_numbers-- 自减了GuessResult.length次,all_numbers-1,就会小于0了,All_numbers[all_numbers - 1]就会报ArrayIndexOutOfBoundsException啦
all_numbers--;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我猜 你选的数超过了最大数
追问
真没有……干嘛跟自己过不去啊…………
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询