如何能把这个数组的最后那个数字处理了?

baar 2008-11-02 11:19:01
这是一个求一个数组里面的质数代码,但是我总是不能把数组的最后一个数字处理,把那两个FOR循环的参数调来调去,老是出现"数组越界"的提示.请高人帮帮忙,看看怎样解决这个问题?

class BvP{
static int[] a={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
static int j=0;
public static int [] BdP(int[] b){
for(int i=0;i<a.length;i++){

for(j=i+1;j<a.length-1;j++){

if(a[j]==0){
j=j+1;
}
if(a[i]==0){
i=i+1;

}else if(a[j]%a[i]==0){
a[j]=0;
}
}
System.out.print(a[i]+" ");
}
return b;
}
public static void main(String[] args){
BdP(a);
}
}

谢谢!
...全文
72 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
baar 2008-11-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 AWUSOFT 的回复:]
你判断这个质数的算法好像也有点问题吧?
[/Quote]
你的算法的确比我那个好,我那个想的复杂了.....
baar 2008-11-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 huoyin 的回复:]
因为你的19刚好是一个质数,并且内循环的判断条件为j < a.length-1,我修改了一下你的程序如下:

Java code
class BvP{
static int[] a = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

static int j = 0;

public static int[] BdP(int[] b) {
for (int i = 0; i < a.length; i++) {

for (j = i + 1; j < a.length; j++) {

if …
[/Quote]
谢谢回复,请问为什么在第一个IF里面加了j < a.length-1
就可以把20计算进去呢?
谢谢
baar 2008-11-03
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 AWUSOFT 的回复:]
19你还要加1嘛
[/Quote]
你是指I在这句话
.....
if(a[i]==0){
i=i+1;

}
...
里面加1?
awusoft 2008-11-03
  • 打赏
  • 举报
回复
19你还要加1嘛
baar 2008-11-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 AWUSOFT 的回复:]
i=a.length;-->这个length是20吧
a[20]不是错了吗?
[/Quote]
length是表示数组长度吧?我是从2开始的,没有把1算进来,里面一共有19个元素,所以length应该等于19吧?
awusoft 2008-11-03
  • 打赏
  • 举报
回复
为什么不给我分啊??????????????????
awusoft 2008-11-02
  • 打赏
  • 举报
回复
i=a.length;-->这个length是20吧
a[20]不是错了吗?
gmh521 2008-11-02
  • 打赏
  • 举报
回复
class BvP{
static int[] a={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
static int j=0;
public static int [] BdP(int[] b){
for(int i=0;i <a.length;i++){
for(j=2;j <a[i];j++){
if(a[i]%j==0){
break;
}
}
if(j>=a[i])
System.out.print(a[i]+" ");
}
return b;
}
public static void main(String[] args){
BdP(a);
}
}
huoyin 2008-11-02
  • 打赏
  • 举报
回复
因为你的19刚好是一个质数,并且内循环的判断条件为j < a.length-1,我修改了一下你的程序如下:

class BvP{
static int[] a = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

static int j = 0;

public static int[] BdP(int[] b) {
for (int i = 0; i < a.length; i++) {

for (j = i + 1; j < a.length; j++) {

if (a[j] == 0 && j < a.length-1) {
j = j + 1;
}
if (a[i] == 0) {
i = i + 1;
} else if (a[j] % a[i] == 0) {
a[j] = 0;
}
}
if(a[i]!=0){
System.out.print(a[i] + " ");
}
}
return b;
}

public static void main(String[] args) {
BdP(a);
}
}

baar 2008-11-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 AWUSOFT 的回复:]
因为你有了一个
i=i+1;
你后边又来了一个
a[i]

假设i=a.length-1;这时候是正确的,循环还在执行
然后是i=i+1;
这时候你再去[/Quote]

谢谢回复,但是我还是不是很明白你的解析,我不知道你说的是哪一行,还有,为什么i=a.length时
访问a[i]这样就会报越界呢?i=a.length时,i不是等于19吗?a[19]不是等于20吗?
kerry_lulu 2008-11-02
  • 打赏
  • 举报
回复
没看懂这程序啥意思...
ssqtjffcu 2008-11-02
  • 打赏
  • 举报
回复
看不懂....
awusoft 2008-11-02
  • 打赏
  • 举报
回复

class BvP{
static int[] a={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
static int j=0;
public static int [] BdP(int[] b){
for(int i=0;i <a.length;i++){
for(j=2;j <a[i];j++){
if(a[i]%j==0){
a[i]=0;
}
}
System.out.print(a[i]+" ");
}
return b;
}
public static void main(String[] args){
BdP(a);
}
}
awusoft 2008-11-02
  • 打赏
  • 举报
回复
你判断这个质数的算法好像也有点问题吧?
awusoft 2008-11-02
  • 打赏
  • 举报
回复
因为你有了一个
i=i+1;
你后边又来了一个
a[i]

假设i=a.length-1;这时候是正确的,循环还在执行
然后是i=i+1;
这时候i=a.length
你再去访问a[i]这样就会报越界了

62,614

社区成员

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

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