2013年10月5日星期六

Number three back a program error please advice ~ Thank

package com.test2;
/ / return a number three
public class Count3Quit {
public static void main (String [] args) {
boolean [] a = new boolean [500];
for (int i = 0; i int index;
int count = 0;
int len ​​= a.length;
for (index = 0; len> = 1; index + +) {
if (a [index] == true) {
count + +;
if (count == 3) {count = 0; a [index] = false; len -;}
}
if (index == a.length) {index = 0;}
}

for (int i = 0; i if (a [i] == true) {System.out.print (i +1);}
}
}
}


error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 500
at com.test2.Count3Quit.main (Count3Quit.java: 16)

My question is : This for loop end condition to write : for (index = 0; len> = 1; index + +) can it ? Error display array bounds , I do not understand where the problem is a lot of advice please newcomers also grateful ~
------ Solution ------------ --------------------------------
teenager, array subscript is starting from 0 . 500 defines an array subscript 0-499 ,
your code will certainly keep cycling to a.length, is 500, a [500] has crossed the line.
------ Solution ---------------------------------------- ----
above a little problem


public class Count3Quit {
public static void main(String[] args) {
boolean[] a = new boolean[500];
for (int i = 0; i < a.length; i++) {
a[i] = true;
}
int index;
int count = 0;
int len = a.length;
for (index = 0; len > 1; index++) {
if (a[index] == true) {
count++;
if (count == 3) {
count = 0;
a[index] = false;
len--;
}
}
if (index == a.length - 1) {
index = -1;
}
}

for (int i = 0; i < a.length; i++) {
if (a[i] == true) {
System.out.print(i + 1);
}
}
}
}

------ For reference only ----------------------------------- ----

不太明白楼主的意图,不过感觉应该这么改
if(index==a.length){index=0;}
}
改为
if (index == a.length - 1) {
//index = 0;
break;
}

------ For reference only ---------------------------- -----------

this is based on the site http://blog.csdn.net/elifefly/article/details/3459748
in the first method change, I just want to ask so that okay . Here, however, do not break, you want to index == a.length back to 0 when the index calculation .

------ For reference only ---------------------------------- -----

you say is not for (index = 0; len> = 1; index + +) where len> = 1 as the end of the statement could not exist ?
------ For reference only -------------------------------------- -

this is based on the site http://blog.csdn.net/elifefly/article/details/3459748   
in the first method change, I just want to ask so that okay . Here, however, do not break, you want to index == a.length back to 0 when the index calculation .   
 



if(index==a.length){
    index=0;
}
改为
if (index == a.length - 1) {
index = 0;
}
if(len == 1){
break;
}

------ For reference only ----------------------------------- ----

this is based on the site http://blog.csdn.net/elifefly/article/details/3459748     
in the first method change, I just want to ask so that okay . Here, however, do not break, you want to index == a.length back to 0 when the index calculation .     
         
  
  
  

if(index==a.length){
    index=0;
}
改为
if (index == a.length - 1) {
index = 0;
}
if(len == 1){
break;
}
 

I have not installed on this computer environments Try again tomorrow thank you very much ~
------ For reference only --------------- ------------------------

public class Count3Quit {
public static void main(String[] args) {
boolean[] a = new boolean[500];
for (int i = 0; i < a.length; i++) {
a[i] = true;
}
int index;
int count = 0;
int len = a.length;
for (index = 0; len > 1; index++) {
if (a[index] == true) {
count++;
if (count == 3) {
count = 0;
a[index] = false;
len--;
}
}
if (index == a.length - 1) {
index = 0;
}
}

for (int i = 0; i < a.length; i++) {
if (a[i] == true) {
System.out.print(i + 1);
}
}
}
}

------ For reference only -------- -------------------------------
 would like to thank pointing !

没有评论:

发表评论