用户交互Scanner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.wang.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()){ String str = scanner.next(); System.out.println("输入的内容为: "+str); } scanner.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.wang.scanner;
import java.util.Scanner;
public class Demo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("使用nextLine方式接收: ");
if (scanner.hasNextLine()){ String str = scanner.nextLine(); System.out.println("输出的内容为: "+str); } scanner.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.wang.scanner;
import java.util.Scanner;
public class Demo03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入数据: ");
String str = scanner.nextLine(); System.out.println("输出的内容为: "+str);
scanner.close(); } }
|
Scanner进阶使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.wang.scanner;
import java.util.Scanner;
public class Demo04 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
int i = 0; float f = 0.0f; System.out.println("请输入整数: ");
if (scanner.hasNextInt()){ i = scanner.nextInt(); System.out.println("整数数据: "+i); }else { System.out.println("输入的不是整数数据!"); }
System.out.println("请输入小数: ");
if (scanner.hasNextFloat()){ f = scanner.nextFloat(); System.out.println("小数数据: "+f); }else { System.out.println("输入的不是小数数据!"); }
scanner.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.wang.scanner;
import java.util.Scanner;
public class Demo05 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
double sum = 0; int m = 0; while (scanner.hasNextDouble()){ double x = scanner.nextDouble(); m = m + 1; sum = sum +x; System.out.println("你输入了第"+m+"个数据,然后当前结果为sum="+sum); } System.out.println(m + "个数的和为" +sum); System.out.println(m + "个数的平均值是" +(sum/m));
scanner.close(); } }
|
顺序结构
1 2 3 4 5 6 7 8 9 10 11
| package com.wang.struct;
public class ShunXuDemo { public static void main(String[] args) { System.out.println("hello1"); System.out.println("hello2"); System.out.println("hello3"); System.out.println("hello4"); } }
|
选择结构
if单选择结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.wang.struct;
import java.util.Scanner;
public class IfDemo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容: "); String s = scanner.nextLine();
if (s.equals("Hello")) { System.out.println(s); } System.out.println("End"); scanner.close(); } }
|
if双选择结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.wang.struct;
import java.util.Scanner;
public class IfDemo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩: "); int score = scanner.nextInt();
if (score>=60){ System.out.println("及格"); }else{ System.out.println("不及格"); } scanner.close(); } }
|
if多选择结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package com.wang.struct;
import java.util.Scanner;
public class IfDemo03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:"); int score = scanner.nextInt();
if (score==100){ System.out.println("恭喜满分"); }else if (score<100 && score>=90){ System.out.println("A级"); }else if (score<90 && score>=80){ System.out.println("B级"); }else if (score<80 && score>=70){ System.out.println("C级"); }else if (score<70 && score>=60){ System.out.println("D级"); }else if (score<60 && score>=0){ System.out.println("不及格"); }else { System.out.println("成绩不合法"); } scanner.close(); } }
|
嵌套的if结构
switch多选择结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.wang.struct;
public class SwitchDemo01 { public static void main(String[] args) { char grade = 'C';
switch (grade){ case 'A': System.out.println("优秀"); break; case 'B': System.out.println("良好"); break; case 'C': System.out.println("及格"); break; case 'D': System.out.println("再接再厉"); break; case 'E': System.out.println("挂科"); } } }
|
switch支持字符串String类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.wang.struct;
public class SwitchDemo02 { public static void main(String[] args) { String name = "王同学";
switch (name){ case "王同学": System.out.println("王同学"); break; case "阿龙": System.out.println("阿龙"); break; default: System.out.println("弄啥嘞!"); } } }
|
IDEA反编译:
右击包–Open in–Explorer–将对应的class文件丢进包中,即可实现反编译查看源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
package com.wang.struct;
public class SwitchDemo02 { public SwitchDemo02() { }
public static void main(String[] args) { String name = "王同学"; byte var3 = -1; switch(name.hashCode()) { case 1233210: if (name.equals("阿龙")) { var3 = 1; } break; case 29115813: if (name.equals("王同学")) { var3 = 0; } }
switch(var3) { case 0: System.out.println("王同学"); break; case 1: System.out.println("阿龙"); break; default: System.out.println("弄啥嘞!"); }
} }
|
循环结构
while循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.wang.struct;
public class WhileDemo01 { public static void main(String[] args) {
int i = 0;
while (i<100){ i++; System.out.println(i); } } }
|
伪代码:
1 2 3 4 5 6 7 8 9 10 11 12
| package com.wang.struct;
public class WhileDemo02 { public static void main(String[] args) { while (true){ } } }
|
计算1+2+3+…+100=?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.wang.struct;
public class WhileDemo03 { public static void main(String[] args) {
int i = 0; int sum = 0;
while (i<=100){ sum = sum +i; i++; } System.out.println(sum); } }
|
do…while循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.wang.struct;
public class DoWhileDemo01 { public static void main(String[] args) { int i = 0; int sum = 0;
do { sum = sum + i; i++; }while (i<=100); System.out.println(sum); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.wang.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循环
IDEA快捷方式:num.for
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.wang.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循环结束!");
for (int i=1;i<=100;i++){ System.out.println(i); } System.out.println("for循环结束!"); } }
|
关于for循环有以下几点说明:
- 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
- 然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
- 执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
- 再次检测布尔表达式。循环执行上面的过程。
练习1:计算0到100之间的奇数和偶数的和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.wang.struct;
public class ForDemo02 { public static void main(String[] args) {
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("奇数的和: "+oddSum); System.out.println("偶数的和: "+evenSum); } }
|
练习2:用while或for循环输出1—1000之间能被5整除的数,并且每行输出3个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.wang.struct;
public class ForDemo03 { public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) { if(i%5==0){ System.out.print(i+"\t"); } if (i%(5*3)==0) { System.out.println("\n"); } } } }
|
练习3:打印九九乘法表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.wang.struct;
public class ForDemo04 { public static void main(String[] args) {
for (int j = 1; j <= 9; j++) { for (int i = 1; i <= j; i++) { System.out.print(i+"*"+j+"="+(j*i) +"\t"); } System.out.println(); } } }
|
增强for循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.wang.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.wang.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; } } } }
|
continue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.wang.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.println(i); } } }
|
带有label的continue,不建议使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.wang.struct;
public class LabelDemo { public static void main(String[] args) {
int count = 0;
outer: for (int i=101;i<150;i++){ for (int j = 2;j<i/2;j++){ if(i % j == 0){ continue outer; } } System.out.print(i+" "); } } }
|
流程控制练习
打印5行三角形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.wang.struct;
public class TestDemo { public static void main(String[] args) {
for (int i = 1; i <= 5; i++) { for (int j = 5; j >= i; j--){ System.out.print(" "); } for (int j = 1; j <= i; j++){ System.out.print("*"); } for (int j = 1; j < i; j++){ System.out.print("*"); } System.out.println(); } } }
|