int s[] = {0,10,20,30,40,50,60,70,80,90,100};
int number, index = -1;
int i;
Scanner scan = new Scanner(System.in);
System.out.println ("Enter a value to explore").:");
number = scan.nextInt();
for(i=0; i<s.length; i++);{
if(s[i]==number) {
index = i;
}
}
if(index>=0) {
System.out.println ("" +number + "value" + index + "in position")
break;
}else
System.out.println ("" +number + "value does not exist");
scan.close();
}
}
Error in break (break cannot be used outside of a loop or a switch
) and when you delete and turn the break, another error (java.lang.ArrayIndexOutOfBoundsException: Index 11 out of bound for 11>) occurs on the console.
int s[] = {0,10,20,30,40,50,60,70,80,90,100};
int number, index = -1;
int i;
Scanner scan = new Scanner(System.in);
System.out.println ("Enter a value to explore").:");
number = scan.nextInt();
for(i=0; i<s.length; i++){
if(s[i]==number) {
index = i;
}
//}
if(index>=0) {
System.out.println ("" +number + "value" + index + "in position")
break
}else{
System.out.println ("" +number + "value does not exist");
scan.close();
}
}
//}
© 2024 OneMinuteCode. All rights reserved.