From 8e87792e1e343dcd852c1520c5b1a6da731944ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=AD=90=E8=B1=AA?= <2936219414@qq.com> Date: Wed, 29 Mar 2023 21:45:22 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Java=E9=9D=A2=E5=90=91=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\2321.md" | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 "54.\345\217\266\345\255\220\350\261\252/\344\275\234\344\270\2321.md" diff --git "a/54.\345\217\266\345\255\220\350\261\252/\344\275\234\344\270\2321.md" "b/54.\345\217\266\345\255\220\350\261\252/\344\275\234\344\270\2321.md" new file mode 100644 index 0000000..e35d2f4 --- /dev/null +++ "b/54.\345\217\266\345\255\220\350\261\252/\344\275\234\344\270\2321.md" @@ -0,0 +1,139 @@ +**判断一个字符数据是否是数字字符** + +1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 + +2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0,并且还要下于或等于字符9即可。 + +3、判断完成之后,打印判断的结果。 + +```java +import java.util.Scanner; +public class zy22{ + public static void main(String[] args) { + Scanner num=new Scanner(System.in); + System.out.println("请输入1-9中的其中一个数字"); + int n=num.nextInt(); + if(0<=n & n<=9){ + System.out.println("这个字符是数字字符"); + }else{ + System.out.println("这个字符不是数字字符"); + } + } +} +``` + +**判断一个字符数据是否是字母字符** + +1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 + +2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符,因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z + +3、判断完成之后,打印判断的结果。 + +```java + +import java.util.Scanner; +public class zy23 { + public static void main(String[] args) { + Scanner num=new Scanner(System.in); + System.out.println("请输入1-9中的其中一个数字"); + int n=num.nextInt(); + if(0<=n & n<=9){ + System.out.println("这个字符是数字字符"); + }else{ + System.out.println("这个字符不是数字字符"); + } + } +} +``` + +**判断指定的年份是否为闰年,请使用键盘录入** + +1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 + +2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 + +```java +import java.util.Scanner; +public class zy24 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入年份"); + int n= sc.nextInt(); + if(n%4==0 && n%100!=0 || n%400==0){ + System.out.println("是闰年"); + }else{ + System.out.println("不是闰年"); + } + } + } + +``` + +**判断一个数字是否为水仙花数,请使用键盘录入** + +1、首先需要提供一个需要判断的3位数字,因此需要一个数值 + +2、判断的过程 + +a) 将3位数字的每一位上的数字拆分下来 + +b) 计算每位数字的3次幂之和 + +C) 用和值 和 原来的数字进行比较 + +D) 打印判断的比较结果即可 + +```java +import java.util.Scanner; +public class zy25 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入3位数"); + int sz= sc.nextInt(); + int gw=sz%10; + int sw=sz/10%10; + int bw=sz/100; + if(sz==gw*gw*gw+sw*sw*sw+bw*bw*bw){ + System.out.println("你输入的是水仙花数"); + }else{ + System.out.println("你输入的不是水仙花数"); + } + } +} +``` + +**判断一个5位数字是否为回文数,使用键盘录入** + +五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 + +1、首先需要提供一个需要判断的5位数字,因此需要一个数值 + +2、判断的过程 + +a) 将5位数字的万、千、十、个位数拆分出来 + +b) 判断比较万位和个位 、 千位和十位是否相等 + +3、判断完成之后,打印判断的结果。 + +```java +import java.util.Scanner; +public class zy27 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入五位数"); + int sz= sc.nextInt(); + int ww=sz/10000;//万位 + int qw=sz%10000/1000;//千位 + int gw=sz%10;//个位 + int sw=sz%100/10;//十位 + if(ww==gw && sw==qw){ + System.out.println("是回文数"); + }else{ + System.out.println("不是回文数"); + } + } +} +``` + -- Gitee From 2d02cb48c8bdf4ccf7db088ba82812e35235931a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=AD=90=E8=B1=AA?= <2936219414@qq.com> Date: Thu, 6 Apr 2023 09:38:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\2322.md" | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 "54.\345\217\266\345\255\220\350\261\252/\344\275\234\344\270\2322.md" diff --git "a/54.\345\217\266\345\255\220\350\261\252/\344\275\234\344\270\2322.md" "b/54.\345\217\266\345\255\220\350\261\252/\344\275\234\344\270\2322.md" new file mode 100644 index 0000000..3719f9d --- /dev/null +++ "b/54.\345\217\266\345\255\220\350\261\252/\344\275\234\344\270\2322.md" @@ -0,0 +1,142 @@ +1.定义5个元素数组 + + 2.可以使用初始化数组的两种方式之一为数组元素赋值 + + 3.遍历数组求数组中的最小值 + +```java +import java.util.Scanner; + +public class D1 { + public static void main(String[] args) { + int[] num =new int[5]; + for (int i = 0; i < num.length; i++) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入"+(i+1)); + num[i]=sc.nextInt(); + } + int min = num[0]; + for (int i = 0; i num[i]){ + min=num[i]; + } + } + System.out.println("最小的是"+min); + } + } +``` + +**2.需求:求出数组中索引与索引对应的元素都是奇数的元素** + +**分析:** + + 1、遍历数组 + + 2、判断索引是否是奇数(索引 % 2 != 0) + + 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) + + 4、满足条件输出结果 + +```java +public class D2 { + public static void main(String[] args) { +// 1、遍历数组 +// +// 2、判断索引是否是奇数(索引 % 2 != 0) +// +// 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) +// +// 4、满足条件输出结果 + int[] arr ={12,11,17,4,6}; + for (int i = 0; i < arr.length; i++) { + if (i%2!=0 && arr[i]%2!=0){ + System.out.println("基数是"+arr[i]); + + } + } + + } +} +``` + +**3.按要求在main方法中完成以下功能:** + + a. 定义一个长度为5的int型数组arr,提示用户输入5个1-60之间的数字作为数组元素 + + b. 生成2-10(范围包含2和10)之间的随机数num + + c. 遍历数组arr,筛选出数组中不是num倍数的元素并输出 + + **PS:输入的数组元素范围包括1和60,不需要代码判断** + +```java +import java.util.Random; +import java.util.Scanner; + +public class D3 { + public static void main(String[] args) { + int[] arr = new int[5]; + for (int i = 0; i < arr.length; i++) { + Scanner sc= new Scanner(System.in); + System.out.println("请输入1-60的数"+(i+1)+":"); + arr[i]=sc.nextInt(); + } + Random ran=new Random(); + int num =ran.nextInt(8)+2; + for (int i = 0; i < arr.length; i++) { + if (arr[i]%num!=0){ + System.out.println(arr[i]+"不是"+num+"的倍数"); + } + } + + } +} +``` + +**4.有一个数组int[] arr = {9,1,3,4,54,56,23,22,20,43,45,78};,要求打印数组中能被6整除的元素。** + +```java +public class D4 { + public static void main(String[] args) { +// **4.有一个数组int[] arr = {9,1,3,4,54,56,23,22,20,43,45,78};,要求打印数组中能被6整除的元素。** + int[] arr ={9,1,3,4,54,56,23,22,20,43,45,78}; + System.out.print("能被6整除的元素有:"); + for (int i = 0; i < arr.length; i++) { + if (arr[i]%6==0){ + System.out.print(+arr[i]+" "); + + } + + } + } +} +``` + +**5.定义一个长度为20的数组,元素为20-40的随机数,要求判断指定元素在数组中出现的次**数,指定元素为键盘录入范围为20-40之间。 + +```java +import java.util.Random; +import java.util.Scanner; + +public class D5 { + public static void main(String[] args) { +// **5.定义一个长度为20的数组,元素为20-40的随机数,要求判断指定元素在数组中出现的次**数,指定元素为键盘录入范围为20-40之间。 + int[] arr = new int[20]; + Scanner sc= new Scanner(System.in); + System.out.println("输入20-40的数:"); + int a=sc.nextInt(); + Random ran =new Random(); + for (int i = 0; i < arr.length; i++) { + arr[i] = ran.nextInt(21)+20; + } + int count=0; + for (int i = 0; i