我修改了一下,寶寶︰public class Break
{
public static void main(String arg[]){
int[] arrayOfInts={32,74,3,129,54,2000};
int searchfor=3;
int i=0;
boolean foundIt=false;
for (i=0;i<=5;i++) {
if(arrayOfInts[i]==searchfor){
foundIt=true;
break;
}
}
if (foundIt){
System.out.println("Found"+searchfor+"at index"+(i+1));
}
else
{
System.out.println(searchfor+"not in the array");
}
}
{
public static void main(String arg[]){
int[] arrayOfInts={32,74,3,129,54,2000};
int searchfor=3;
int i=0;
boolean foundIt=false;
for (i=0;i<=5;i++) {
if(arrayOfInts[i]==searchfor){
foundIt=true;
break;
}
}
if (foundIt){
System.out.println("Found"+searchfor+"at index"+(i+1));
}
else
{
System.out.println(searchfor+"not in the array");
}
}
System.out.println(searchfor+"not in the array");
有错
找不到 怎么还能 用searchfor那
public static void main(String arg[]){
int[] arrayOfInts={32,74,3,129,54,2000};
int searchfor=3;
int i=0;
boolean foundIt=false;
for (i=0;i<arrayOfInts.length;i++){
if(arrayOfInts[i]==searchfor){
foundIt=true;
break;
}
}
if (foundIt){
System.out.println("Found"+searchfor+"at index"+i);
}else{
System.out.println(searchfor+"not in the array");
}
}
}
{
public static void main(String arg[]){
int[] arrayOfInts={32,74,3,129,54,2000};
int searchfor=3;
int i=0;
boolean foundIt=false;
for (i=0;i<=arrayOfInts.length-1;i++) {
if(arrayOfInts[i]==searchfor){
foundIt=true;
break;
}
}
if (foundIt)
System.out.println("Found"+searchfor+"at index"+(i+1));
else
System.out.println(searchfor+"not in the array");
}
}這回才是個比較理想的程序,寶寶
建议你循环变量在循环体中定义
如for (i=0;i<arrayOfInts.length;i++)可以写成
for (int i=0;i<arrayOfInts.length;i++),
而不要单独写int i=0,再写for循环体。