From 722ec41fd2fb112d03ac1356c26c9e3cd8fd28ea Mon Sep 17 00:00:00 2001 From: CZHXX <14938243+czhxx@user.noreply.gitee.com> Date: Mon, 14 Oct 2024 03:35:00 +0000 Subject: [PATCH] w w Signed-off-by: CZHXX <14938243+czhxx@user.noreply.gitee.com> --- ForDemo1.java | 35 +++++++++++++++++++++++++++++++++++ ForDemo2.java | 21 +++++++++++++++++++++ ForDemo3.java | 21 +++++++++++++++++++++ IfDemo1.java | 18 ++++++++++++++++++ IfDemo2.java | 16 ++++++++++++++++ Student.java | 38 ++++++++++++++++++++++++++++++++++++++ da.java | 12 ++++++++++++ 7 files changed, 161 insertions(+) create mode 100644 ForDemo1.java create mode 100644 ForDemo2.java create mode 100644 ForDemo3.java create mode 100644 IfDemo1.java create mode 100644 IfDemo2.java create mode 100644 Student.java create mode 100644 da.java diff --git a/ForDemo1.java b/ForDemo1.java new file mode 100644 index 000000000..da881ebb3 --- /dev/null +++ b/ForDemo1.java @@ -0,0 +1,35 @@ +public class ForDemo1 { + public static void main(String[] args) { + System.out.println("----3次Hello World----"); + for (int i = 0; i < 3; i++) { + if (i == 1) { + break; // 当i等于1时,跳出循环 + } + System.out.println("Hello World"); + } + + System.out.println("----4次Hello MySQL----"); + for (int i = 1; i < 5; i++) { + if (i == 3) { + continue; // 当i等于3时,跳过当前迭代 + } + System.out.println("Hello MySQL"); + } + + System.out.println("----5次Hello Spring----"); + for (int i = 1; i <= 5; i++) { + if (i == 3) { + continue; // 当i等于3时,跳过当前迭代 + } + System.out.println("Hello Spring"); + } + + System.out.println("----3次Hello Redis----"); + for (int i = 1; i <= 5; i += 2) { + if (i == 3) { + break; // 当i等于3时,跳出循环 + } + System.out.println("Hello Redis"); + } + } +} diff --git a/ForDemo2.java b/ForDemo2.java new file mode 100644 index 000000000..254408732 --- /dev/null +++ b/ForDemo2.java @@ -0,0 +1,21 @@ +public class ForDemo2 { + public static void main(String[] args){ + int [] numbers = {10, 20, 30, 40, 50}; + for(int x : numbers ){ + if (x == 30) { + break; // 当x等于30时,跳出循环 + } + System.out.print( x ); + System.out.print(","); + } + System.out.println(); + String [] names ={"James", "Larry", "Tom", "Lacy"}; + for( String name : names ) { + if (name.equals("Tom")) { + continue; // 当name等于"Tom"时,跳过当前循环,继续下一个循环 + } + System.out.print( name ); + System.out.print(","); + } + } +} diff --git a/ForDemo3.java b/ForDemo3.java new file mode 100644 index 000000000..bf452b6aa --- /dev/null +++ b/ForDemo3.java @@ -0,0 +1,21 @@ +public class ForDemo3 { + public static void main(String[] args) { + // 使用break + System.out.println("----使用break----"); + for (int i = 1; i <= 5; i++) { + if (i == 3) { + break; + } + System.out.println("Hello Break"); + } + + // 使用continue + System.out.println("----使用continue----"); + for (int i = 1; i <= 5; i++) { + if (i == 3) { + continue; + } + System.out.println("Hello Continue"); + } + } +} diff --git a/IfDemo1.java b/IfDemo1.java new file mode 100644 index 000000000..b9776de43 --- /dev/null +++ b/IfDemo1.java @@ -0,0 +1,18 @@ +public class IfDemo1 { + public static void main(String[] args) { + // 格式3: if(条件表达式){ 代码... } else if(条件表达式){ 代码... } ... else{ 代码... } + int score = 95; + // 绩效:0-60 C,60-80 B,80-90 A,90-100 A+ + if (score >= 0 && score < 60) { + System.out.println("分数是:" + score + ",绩效是:C"); + } else if (score >= 60 && score < 80) { + System.out.println("分数是:" + score + ",绩效是:B"); + } else if (score >= 80 && score < 90) { + System.out.println("分数是:" + score + ",绩效是:A"); + } else if (score >= 90 && score <= 100) { + System.out.println("分数是:" + score + ",绩效是:A+"); + } else { + System.out.println("录入的分数有毛病!"); + } + } +} \ No newline at end of file diff --git a/IfDemo2.java b/IfDemo2.java new file mode 100644 index 000000000..0080f8f70 --- /dev/null +++ b/IfDemo2.java @@ -0,0 +1,16 @@ +public class IfDemo2 { + public static void main(String[] args) { + // 嵌套的 if…else 语句 + int x = 30; + int y = 10; + if (x == 30) { + if (y == 20) { + System.out.print("X = 30 and Y = 20"); + } else { + System.out.println("X = 30 and Y != 20"); + } + } else { + System.out.println("X != 30"); + } + } +} diff --git a/Student.java b/Student.java new file mode 100644 index 000000000..5c1a9fc18 --- /dev/null +++ b/Student.java @@ -0,0 +1,38 @@ +public class Student { + // 1.局部变量:在方法、构造函数或块内部声明的变量。 + public void run() { + String weight = "100斤"; // 局部变量weight,必须有初始值。 + // 因为实例变量age没赋值,所以默认是0 + System.out.println(name + "现在" + age + "岁"); // 小明现在0岁 + System.out.println(name + weight + ",跑得很快"); // 小明100斤,跑得很快 + } + + // 2.实例变量:在类中声明,但在方法、构造函数或块之外。 + public String name = "小明"; // 公有实例变量name对子类可见 + private int age; // 私有实例变量age,仅在该类可见,不赋值,默认是0 + + // main方法,程序入口 + public static void main(String[] args) { + Student.onLineNumber++; // 22 + System.out.println(onLineNumber); + Student student = new Student(); + student.run(); + student.eat("方便面"); + student.study(); + } + + // 3.类变量:在类中用 static 关键字声明的变量,它们属于类而不是实例。 + public static int onLineNumber = 21; + private String major = "计算机科学"; + + // 4.参数变量:是方法或构造函数声明中的变量 + public void eat(String food) { // 参数变量food + System.out.println(name + "喜欢吃" + food); // 小明喜欢吃方便面 + } + + // 自定义方法 + public void study() { + System.out.println(name + "正在学习" + major); + } +} + diff --git a/da.java b/da.java new file mode 100644 index 000000000..dec1539b3 --- /dev/null +++ b/da.java @@ -0,0 +1,12 @@ +public class da { + public static void main(String[] args) { + int n = 7; + if (n >= 0 && n < 10) { + for (int i = 0; i < n; i++) { + System.out.println("He" + n + n +"o" + " Wor" + n + "d"); + } + } else { + System.out.println("n>10"); + } + } +}