给出一个数组,输入一个数判断该书是数组中的第几个或者判断该数是否在数组中
package exercise_1;
import java.util.Scanner;
public class foreach {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int[] data = {3,5,56,8,5,6,2,5};
int x = in.nextInt();
int loc = 1;
for(int i = 0;i<data.length;i++)
{
if(x == data[i])
{
loc = i;
break;
}
}
if(loc >-1)
{
System.out.println(x+"是第"+(loc+1)+"个");
}
else
{
System.out.println(x+"不在其中");
}
}
}
// boolean found = false;
// for(int k : data ) //foreach循环每次循环将数组中的值赋值给k
// //但是不能将其他数去给数组赋值
// //也不能得出数是第几个
// {
// if(k == x)
// {
// found = true;
//
// }
// }
// if(found)
// {
// System.out.println(x+"在其中");
// }
// else
// {
// System.out.println(x="不在其中");
// }