java流程控制(依然还是没有图片,不过不大事)
java流程控制
用户交互scanner
使用next方式接收:
package com.maru.scanner; import java.util.Scanner; public class Demo01 { public static void main(String[] args) { //创建一个扫描器对象,用于接收键盘数据 Scanner scanner = new Scanner(System.in); System.out.println(使用next方式接收:); //判断用户有没有输入字符串 if (scanner.hasNext()){ //使用next方式接受 String str = scanner.next();//程序会等待用户输入完毕 System.out.println(输出的内容为字符串;+str ); } //凡是属于IO流的类如果不关闭就会一直占用资源,要养成好习惯用完就关掉 scanner.close(); } }
使用nextline方式接收:if可以去掉
package com.maru.scanner; import java.util.Scanner; public class Demo01 { public static void main(String[] args) { //创建一个扫描器对象,用于接收键盘数据 Scanner scanner = new Scanner(System.in); System.out.println(使用next方式接收:); //判断用户有没有输入字符串 if (scanner.hasNext()){ //使用next方式接受 String str = scanner.next();//程序会等待用户输入完毕 System.out.println(输出的内容为字符串;+str ); } //凡是属于IO流的类如果不关闭就会一直占用资源,要养成好习惯用完就关掉 scanner.close(); } }
选择结构
-
if单选择结构
-
if双选择结构
-
if多选择结构
-
嵌套的if结构
-
switch多选择结构
循环结构
-
while循环
package com.maru.struct; public class whileDemo01 { public static void main(String[] args) { //输出1~100 int i = 0; while (i < 100) { i++; System.out.println(i); } } }
package com.maru.struct; public class whileDemo03 { public static void main(String[] args) { //计算1到100的和 int i = 0; int sum = 0; while (i<=100){ sum = sum + i; i++; } System.out.println(sum); } }
-
do...while循环
区别:
package com.maru.struct; public class DoWhileDemo02 { public static void main(String[] args) { int a = 0; while (a<0){ System.out.println(a); a++; } System.out.println(==============================); do{ System.out.println(a); a++; }while (a<0); } }
- for循环
package com.maru.struct; public class ForDemo01 { public static void main(String[] args) { int a = 1;//初始化条件 while (a <= 100){//条件判断 System.out.println(a);//循环体 a += 2;//迭代 } System.out.println(while循环结束); System.out.println(==================================); //初始化//条件判断//迭代 for (int i = 1;i <= 100;i++){ System.out.println(i); } System.out.println(for循环结束); } }
练习1:
package com.maru.struct; public class ForDemo02 { public static void main(String[] args) { //计算1到100之间奇数和偶数的和 int oddSum = 0; int evenSum = 0; for (int i = 0; i <= 100; i++) { if (i % 2 !=0){ oddSum += i; }else { evenSum += i; } } System.out.println(100以内奇数的和+oddSum); System.out.println(100以内偶数的和+evenSum); } }
练习2:
package com.maru.struct; public class ForDemo03 { public static void main(String[] args) { //1000以内能被5整除的数,并且每行输出3个 System.out.println(1000以内能被5整除的数); int j = 0; for (int i = 0; i <= 1000; i++) { if (i%5==0){ System.out.print(i+\t); j++; if (j==3){ System.out.print(\n); //System.out.println(); j = 0; } } } } }
练习3:
package com.maru.struct; public class ForDemo04 { public static void main(String[] args) { 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(); } } }
- 增强for循环
package com.maru.struct; public class ForDemo05 { public static void main(String[] args) { int[] numbers = {10,20,30,40,50};//定义了一个数组 for (int i = 0; i < 5; i++) { System.out.println(numbers[i]); } System.out.println(======================); //遍历数组的元素 for (int x : numbers) { System.out.println(x); } } }
- break continue
break
package com.maru.struct; public class BreakDemo { public static void main(String[] args) { int i =0; while (i<=100){ i++; System.out.println(i); if (i==30){ break; } } System.out.println(123); } }
continue
package com.maru.struct; public class ContinueDemo { public static void main(String[] args) { int i =0; while (i<100){ i++; if (i%10==0){ System.out.println(); continue; } System.out.print(i+\t); } } }
- 练习:打印一个三角形