w

Signed-off-by: CZHXX <14938243+czhxx@user.noreply.gitee.com>
pull/528/head
CZHXX 2024-10-14 03:35:00 +00:00 committed by Gitee
parent 556d3e9db3
commit 722ec41fd2
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 161 additions and 0 deletions

35
ForDemo1.java Normal file
View File

@ -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");
}
}
}

21
ForDemo2.java Normal file
View File

@ -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(",");
}
}
}

21
ForDemo3.java Normal file
View File

@ -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");
}
}
}

18
IfDemo1.java Normal file
View File

@ -0,0 +1,18 @@
public class IfDemo1 {
public static void main(String[] args) {
// 格式3 if(条件表达式){ 代码... } else if(条件表达式){ 代码... } ... else{ 代码... }
int score = 95;
// 绩效0-60 C60-80 B80-90 A90-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("录入的分数有毛病!");
}
}
}

16
IfDemo2.java Normal file
View File

@ -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");
}
}
}

38
Student.java Normal file
View File

@ -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);
}
}

12
da.java Normal file
View File

@ -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");
}
}
}