From 226ab359fc95d04b9ce06f7fda6144184c26d722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=96=E5=BF=83=E5=A6=8D?= <2392642810@qq.com> Date: Sat, 3 Dec 2022 20:18:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20221203 for\345\276\252\347\216\257.md" | 217 ++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 "03 \350\265\226\345\277\203\345\246\215/20221203 for\345\276\252\347\216\257.md" diff --git "a/03 \350\265\226\345\277\203\345\246\215/20221203 for\345\276\252\347\216\257.md" "b/03 \350\265\226\345\277\203\345\246\215/20221203 for\345\276\252\347\216\257.md" new file mode 100644 index 0000000..04b8527 --- /dev/null +++ "b/03 \350\265\226\345\277\203\345\246\215/20221203 for\345\276\252\347\216\257.md" @@ -0,0 +1,217 @@ +### For循环 + +格式: + +```java +for (初始化语句;条件判断语句;条件控制语句) { + 循环体语句; +} +``` + +#### 一些练习 + + 水仙花数(个十百位相加的立方等于这个数本身),输出两个数然后换行 + +```java +import java.util.Scanner; + +public class D1 { + public static void main(String[] args) { + + //先定值h + int h=0; + + for (int i=100;i<=999;i++){ + + //求个十百位的数值 + int g=i%10; + int s=i/10%10; + int b=i/100; + + //计算水仙花数 + if (g*g*g+s*s*s+b*b*b==i){ + System.out.println(i+" "); + + //h自增让偶数换行 + h++; + if (h%2==0){ + System.out.println(); + } + } + } + } +} +``` + +输出1-10与10-1 + +```java +public class D2 { + public static void main(String[] args) { + + for(int i=1;i<=10;i++){ + System.out.print(i+" "); + } + + System.out.println(); + System.out.println("--------------------"); + + for(int i=10;i>=1;i--){ + System.out.print(i+" "); + } + } +} +``` + +输出26个字母(小写),元音用[ ]框起来 + +```java +public class D3 { + public static void main(String[] args) { + + //ascii码'a'=97,'z'=122 + + for (char i='a';i<='z';i++){ + char c=(char)(97+i); + if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u'){ + System.out.print("[" + i + "] "); + } + } + } +} +``` + +九九乘法表 + +```java +public class D5 { + public static void main(String[] args) { + + //九九乘法表 + //\t是制表 + for(int i=1;i<=9;i++){ + for(int j=1;j<=i;j++){ + System.out.print(j + "x" + i + "=" + i*j + "\t"); + } + System.out.println(); + } + } +} +``` + +求1-10的偶数和 + +```java +public class D6 { + public static void main(String[] args) { + + int a=0; + + for(int i=1;i<=10;i++){ + if (i%2==0){ + a +=i;//a = a + i + } + } + System.out.println(a); + } +} +``` + +**补充** + +用穿透计算此年第几天 + +```java +import java.util.Scanner; + +public class D4 { + public static void main(String[] args) { + + Scanner sc=new Scanner(System.in); + System.out.println("请输入年份"); + int year = sc.nextInt(); + System.out.println("请输入月份"); + int month = sc.nextInt(); + System.out.println("请输入日期"); + int day = sc.nextInt(); + + int num=day; + + switch (month-1){ + case 12: + num+=31; + case 11: + num+=30; + case 10: + num+=31; + case 9: + num+=30; + case 8: + num+=31; + case 7: + num+=31; + case 6: + num+=30; + case 5: + num+=31; + case 4: + num+=30; + case 3: + num+=31; + case 2: + if ((year%4==0 && year%100!=0) || (year %400==0)){ + num+=29; + }else{ + num+=28; + } + case 1: + num+=31; + } + System.out.println(year + "年" + month + "月" + day + "日,是这一年中的第" + num + "天"); + } +} +``` + +### 作业 + +让用户输入一个数字,根据这个数字,循环输出"我爱学习,我爱java",比如,用户输入数字 9 就在控制台输出9行"我爱学习,我爱java" + +```java +import java.util.Scanner; + +public class D1 { + public static void main(String[] args) { + + Scanner sc=new Scanner(System.in); + System.out.println("请输入循环几次"); + int a= sc.nextInt(); + + for (int i=1;i<=a;i++){ + System.out.println("我爱学习,我爱java"); + } + } +} +``` + +用户分别输入数 a,数b,让程序实现从a 开始循环到b.输出"有生之年,能看到中国队出线吗?" + +```java +import java.util.Scanner; + +public class D2 { + public static void main(String[] args) { + + Scanner sc=new Scanner(System.in); + System.out.println("请输入数a"); + int a= sc.nextInt(); + System.out.println("请输入数b"); + int b= sc.nextInt(); + + for(int i=a;i<=b;i++){ + System.out.println("有生之年,能看到中国队出线吗?"); + } + } +} + +``` + -- Gitee