From 530258d81f2a26a3a7b3b77db69b4893079a95dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E5=85=88=E5=B3=BB?= <3458185152@qq.com> Date: Sun, 4 Dec 2022 14:27:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=AC=E4=BA=94=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 --- ...52\347\216\257\350\277\220\347\224\250.md" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "15 \350\260\242\345\205\210\345\263\273/20221204 for\345\276\252\347\216\257\350\277\220\347\224\250.md" diff --git "a/15 \350\260\242\345\205\210\345\263\273/20221204 for\345\276\252\347\216\257\350\277\220\347\224\250.md" "b/15 \350\260\242\345\205\210\345\263\273/20221204 for\345\276\252\347\216\257\350\277\220\347\224\250.md" new file mode 100644 index 0000000..a7c240f --- /dev/null +++ "b/15 \350\260\242\345\205\210\345\263\273/20221204 for\345\276\252\347\216\257\350\277\220\347\224\250.md" @@ -0,0 +1,51 @@ +#### for循环 + +``` java +for(初始化语句;条件判断语句;条件控制语句){ + 循环条件语句 +} +int count = 0 //一但看到求和,求总数,求最大,最小,马上再循环外面定义变量 +``` + +#### 作业1 + +1.让用户输入一个数字,根据这个数字,循环输出"我爱学习,我爱 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"); + } + } + +} + +``` + +#### 作业2 + +2.用户分别输入数 a,数b,让程序实现从a 开始循环到b.输出"有生之年,能看到中国队出线吗?" + +```java +import java.util.Scanner; + +public class D2 { + public static void main(String[] args) { + int count =0; + Scanner sc = new Scanner(System.in); + System.out.println("请输入开始值"); + int a = sc.nextInt(); + System.out.println("请输入结束值"); + int b = sc.nextInt(); + for (int i =a;i<=b;i++){ + count++; + System.out.println("有生之年能看到中国队出线吗?"+count+"次"); + } +``` + -- Gitee From 88d6b465359747072684c38d8ca2e9a34d381248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E5=85=88=E5=B3=BB?= <3458185152@qq.com> Date: Tue, 6 Dec 2022 19:24:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E5=85=AD=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 --- ...20221206 while\345\276\252\347\216\257.md" | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 "15 \350\260\242\345\205\210\345\263\273/20221206 while\345\276\252\347\216\257.md" diff --git "a/15 \350\260\242\345\205\210\345\263\273/20221206 while\345\276\252\347\216\257.md" "b/15 \350\260\242\345\205\210\345\263\273/20221206 while\345\276\252\347\216\257.md" new file mode 100644 index 0000000..a488af7 --- /dev/null +++ "b/15 \350\260\242\345\205\210\345\263\273/20221206 while\345\276\252\347\216\257.md" @@ -0,0 +1,93 @@ +##### 死循环 + +``` java +for(;;){ + sout() +} + +while(true){ + sout +} +do{ + sout() +}while(true) + + break //结束循环; + contiu //跳过,继续下一个; +``` + +##### 随机数 + +```java +Random ran =new Random(); +int num = ran.nextInt(100)+1 // 生成1~100的随机数 +``` + +##### 作业一 + +求555555的约数1中最大1的三位数。 + +```java +public class D4 { + public static void main(String[] args) { + int sum=555555; + int count=0; + for (int i=999;i>=100;i--){ + if (sum%i==0){ + count++; + System.out.println(i); + if (count==1){ + break; + } + } + } + } +} + +``` + +##### 作业二 + +猜数字游戏。由系统随机生成一个0~99的随机数。让用户去猜测,如果太大,系统提示“你猜的数字太大了!再猜”,太小则提示“你猜的数字太小了!再猜”,猜中则提示“恭喜你猜对了!”。 + +猜数字的过程中需要统计用户猜测次数,如果用户一次性猜对,提示“你太厉害了,一次就猜对啦!”,2~6次猜对提示“你很聪明!”,6次以上猜对的话提示“笨笨你怎么猜那么多次!” + +```java +import java.util.Random; +import java.util.Scanner; + +public class D5 { + public static void main(String[] args) { + Random ran = new Random(); + int i = ran.nextInt(100)+1; + Scanner sc = new Scanner(System.in); + int sum = 0; + while (true) { + sum++; + System.out.println("请输入一个100以内的数字:"); + int j = sc.nextInt(); + if (j < i) { + System.out.println("太小了"); + }else if (j > i) { + System.out.println("太大了"); + }else if (j == i & sum==1) { + System.out.println("你太厉害了,一次就答对了!"); + break; + } + else if (j==i && (sum>=2 && sum<=6)){ + System.out.println("你很聪明!"); + break; + }else if (j==i && sum>6){ + + System.out.println("笨笨你怎么猜这么多次!"); + break; + } + } + System.out.println("恭喜你答对了!"); + System.out.println("你一共猜了"+sum+"次"); + + } + } + +``` + -- Gitee