//输入一个值 查找对应数组中元素的索引
import java.util.Scanner;
public class Test06 {
public static void main(String[] args) {
int[] arr = {19, 28, 37, 46, 50};
Scanner sc = new Scanner(System.in);
System.out.println("请输入查找的数据: ");
int number = sc.nextInt();
/*int index = -1;
for(int x = 0; x<arr.length; x++){
if(arr[x] == number){
index = x;
break;
}
} */
int index = getIndex(arr, number);
System.out.println("index: " + index);
}
/*方法调用2个明确:
返回值类型:int
参数:int[] arr,int number
*/
public static int getIndex(int[] arr, int number) {
int index = -1;
for (int x = 0; x < arr.length; x++) {
if (arr[x] == number) {
index = x;
break;
}
}
return index;
}
}
//用方法 判断数组是否相同
public class Test01 {
public static void main(String[] args) {
int[] arr = {11,22,33,44,55,66};
int[] arr2 = {11,22,33,44,55,66};
boolean flag = compare(arr,arr2);
System.out.println(flag);
}
public static boolean compare(int[] arr, int[] arr2){
if(arr.length != arr2.length){
return false;
}
for(int x = 0; x<arr.length; x++){
if(arr[x] != arr2[x]){
return false;
}
}
return true;
}
}
//百钱百鸡
//一个公鸡5钱,一个母鸡3钱,一钱买3个小鸡,,共有100文钱
public class Test03 {
public static void main(String[] args) {
for (int x = 0; x <= 20; x++) {
for (int y = 0; y <= 33; y++) {
int z = 100 - x - y;
if (z % 3 == 0 && 5 * x + 3 * y + z / 3 == 100) {
System.out.println(x + "," + y + "," + z);
}
}
}
}
}
//不死兔
public class Test04 {
public static void main(String[] args) {
int[] arr = new int[20];
arr[0] = 1;
arr[1] = 1;
for(int x=2; x<arr.length;x++){
arr[x] = arr[x-2] + arr[x - 1];
}
System.out.println("第二十个月兔子的对数: " + arr[19]);
}
}
//求数组中数据 个位 十位都不能为7 且能被2整除的数据的和
public class Test02 {
public static void main(String[] args) {
int[] arr = {68,27,95,88,171,996,51,210};
int sum = 0;
for(int x = 0; x<arr.length; x++){
if(arr[x]%10!=7 && arr[x]/10%10!=7 && arr[x]%2 == 0){
sum += arr[x];
}
}
System.out.println("sum: " + sum);
}
}
//1-100之间逢七过
public class Test05 {
public static void main(String[] args) {
for(int x = 1;x <= 100;x++){
if(x%10==7 || x/10%10==7 || x%7 ==0)
System.out.println(x);
}
}
}