mirror of https://gitee.com/y_project/RuoYi.git
parent
556d3e9db3
commit
722ec41fd2
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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(",");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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("录入的分数有毛病!");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue