笔头练习
2021/6/28 6:21:25
本文主要是介绍笔头练习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
//输入一个值 查找对应数组中元素的索引 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); } } }
这篇关于笔头练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求