Pre Merge pull request !528 from CZHXX/master

pull/528/MERGE
CZHXX 2025-02-27 02:57:27 +00:00 committed by Gitee
commit fff4594d9f
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
60 changed files with 1399 additions and 2 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");
}
}
}

13
das/.keep Normal file
View File

@ -0,0 +1,13 @@
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");
}
}
}

View File

@ -8,7 +8,7 @@ spring:
master:
url: jdbc:mysql://localhost:3306/ry?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: password
password: root
# 从库数据源
slave:
# 从数据源开关/默认关闭

View File

@ -16,7 +16,7 @@ ruoyi:
# 开发环境配置
server:
# 服务器的HTTP端口默认为80
port: 80
port: 5555
servlet:
# 应用的访问路径
context-path: /

View File

@ -0,0 +1,10 @@
import java.util.Arrays;
import java.util.List;
public class ArrayToList {
public static void main(String[] args) {
String[] array = {"A", "B", "C", "D"};
List<String> list = Arrays.asList(array);
System.out.println("列表内容: " + list);
}
}

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

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

View File

@ -0,0 +1,13 @@
import java.util.ArrayList;
import java.util.List;
public class ListToArray {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
// 列表转为数组
String[] array = list.toArray(new String[0]);
System.out.println("数组内容: " + list.toString());
}
}

View File

@ -0,0 +1,38 @@
public class NewStudent {
/**
*
*/
private String name;
/**
*
*/
private String age;//定义
/**
*
* @param inputName
* @param inputAge
*/
public NewStudent(String inputName,String inputAge){
this.name = inputName;
this.age = inputAge;
}
public void eat(){
System.out.println("吃饭");
}
/**
*
* @param food
*/
public void eat(String food){
System.out.println("学生吃的"+food);
}
public String eat(String food,String time){
System.out.println("学生在" + time + "吃的" + food);
return"吃饱了";//定义吃饭的方法
}
}

View File

@ -0,0 +1,37 @@
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);
}
}

View File

@ -0,0 +1,4 @@
public class NewStudent {
private String name;
private int age;
}

View File

@ -0,0 +1,37 @@
public class TestArray {
/**
*
* @param args
*/
public static void main(String[] args) {
// double类型的数组
double[] scores = new double[]{99.5, 88.0, 75.5};
double[] scores1 = {99.5, 88.0, 75.5}; // 简化写法
// int类型的数组
int[] ages = new int[]{12, 24, 36};
int[] ages1 = {12, 24, 36}; // 简化写法
int size = 5; // 设置数组大小是10
String[] names = new String[size]; // 定义数组
names[0] = "孙悟空";
names[1] = "贝吉塔";
names[2] = "弗利萨";
names[3] = "沙鲁";
names[4] = "魔人布欧";
// 编译不报错运行报错因为数组大小是5这是数组里第6个元素数组越界ArrayIndexOutOfBoundsException
// names[5] = "布尔玛";
// 数组索引从 0 开始,所以索引值从 0 到 arrayRefVar.length-1
for (int i = 0; i < names.length; i++) {
System.out.print("索引:" + i + names[i] + " ");
}
System.out.println();
// 数组变量名存储的是数组在内存种的地址,数组是引用类型
// [ 指的是 数组
// D 指的是 数据类型double
// @ 指的是 在哪里
System.out.println(scores); // [D@776ec8df,指的是数组的十六进制地址
}
}

View File

@ -0,0 +1,9 @@
public class TestStudent {
public static void main(String[] args){
NewStudent student = new NewStudent("czh","19");
student.eat();
student.eat("sda");
student.eat("sda");
}
}

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

View File

@ -0,0 +1,11 @@
public class ss {
public static void main(String[] args) {
String studentId = "20233011082";
char lastDigit1 = studentId.charAt(studentId.length() - 2);
char lastDigit2 = studentId.charAt(studentId.length() - 1);
char[] lastTwoDigits = {lastDigit1, lastDigit2};
for (char c : lastTwoDigits) {
System.out.print(c + " ");
}
}
}

View File

@ -0,0 +1,39 @@
/**
*
*/
public class Java {
/**
*
*/
private String name1;
private String name2;
/**
* @param name
* @param name2
*/
public Java(String name, String name2) {
this.name1 = name1;
this.name2 = name2;
}
/**
* main
*/
public void java() {
System.out.println(name1 + "cc");
}
public void sha() {
System.out.println(name2 + "xx");
}
}

View File

@ -0,0 +1,9 @@
/**
* A
*/
public class ACoach implements Coach{
@Override
public void command(){
System.out.println("我是A级证书教练");
}
}

View File

@ -0,0 +1,9 @@
/**
* A
*/
public class ACoachFactory implements CoachFactory {
@Override
public Coach createCoach() {
return new ACoach();
}
}

View File

@ -0,0 +1,22 @@
import java.util.Arrays;
import java.util.List;
/**
* ArrayToList
*/
public class ArrayToList {
/**
* main
* @param args
*/
public static void main(String[] args) {
String[] array = {"A", "B", "C", "D"};
/**
*
*/
List<String> list = Arrays.asList(array);
System.out.println("列表内容: " + list);
}
}

View File

@ -0,0 +1,6 @@
/**
*
*/
public interface Book {
String name();
}

View File

@ -0,0 +1,9 @@
/**
*
*/
public interface BookInterface {
String BookName();
String BookAuthor();
String BookISBN();
void setBookInfo(String name, String author, String isbn);
}

View File

@ -0,0 +1,25 @@
public class Booking {
public static void main(String[] args) {
// 创建图书对象
Books books = new Books();
// 设置图书信息
books.setBookInfo("安德的游戏", "安德", "978-7-111-60453-3");
// 获取并打印图书名称
System.out.println("名称: " + books.BookName());
// 获取并打印图书作者
System.out.println("作者: " + books.BookAuthor());
// 获取并打印图书ISBN
System.out.println("ISBN: " + books.BookISBN());
// 使用多态形式调用图书信息类的方法
BookInterface bookInterface = new Books();
bookInterface.setBookInfo("中华上下五千年", "袁堂欣", "978-0-13-468599-1");
System.out.println("名称: " + bookInterface.BookName());
System.out.println("作者: " + bookInterface.BookAuthor());
System.out.println("ISBN: " + bookInterface.BookISBN());
}
}

View File

@ -0,0 +1,27 @@
public class Books implements BookInterface {
private String name;
private String author;
private String isbn;
@Override
public String BookName() {
return name;
}
@Override
public String BookAuthor() {
return author;
}
@Override
public String BookISBN() {
return isbn;
}
@Override
public void setBookInfo(String name, String author, String isbn) {
this.name = name;
this.author = author;
this.isbn = isbn;
}
}

View File

@ -0,0 +1,9 @@
/**
* C
*/
public class CCoach implements Coach{
@Override
public void command() {
System.out.println("我是C级证书教练");
}
}

View File

@ -0,0 +1,9 @@
/**
* C
*/
public class CCoachFactory implements CoachFactory{
@Override
public Coach createCoach() {
return new CCoach();
}
}

View File

@ -0,0 +1,9 @@
/**
* Shape
*/
public class Circle implements Shape {
@Override
public String name() {
return "圆";
}
}

View File

@ -0,0 +1,6 @@
/**
*
*/
public interface Coach {
void command();
}

View File

@ -0,0 +1,6 @@
/**
*
*/
public interface CoachFactory {
Coach createCoach();
}

View File

@ -0,0 +1,12 @@
/**
*
*/
public class Demo {
public static void create(CoachFactory factory) {
factory.createCoach().command();
}
public static void main(String[] args) {
create(new ACoachFactory());
create(new CCoachFactory());
}
}

View File

@ -0,0 +1,55 @@
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) {
/**
*13
*/
break;
}
System.out.println("Hello World");
}
System.out.println("----4次Hello MySQL----");
for (int i = 1; i < 5; i++) {
if (i == 3) {
/**
*13
*/
continue;
}
System.out.println("Hello MySQL");
}
System.out.println("----5次Hello Spring----");
for (int i = 1; i <= 5; i++) {
if (i == 3) {
/**
*i3
*/
continue;
}
System.out.println("Hello Spring");
}
System.out.println("----3次Hello Redis----");
for (int i = 1; i <= 5; i += 2) {
if (i == 3) {
/**
*i3
*/
break;
}
System.out.println("Hello Redis");
}
}
}

View File

@ -0,0 +1,31 @@
public class ForDemo2 {
public static void main(String[] args){
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
if (x == 30) {
/**
*x30
*/
break;
}
System.out.print( x );
System.out.print(",");
}
System.out.println();
String [] names ={"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
if (name.equals("Tom")) {
/**
*name"Tom"
*/
continue;
}
System.out.print( name );
System.out.print(",");
}
}
}

View File

@ -0,0 +1,28 @@
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");
}
}
}

View File

@ -0,0 +1,10 @@
/**
* Book
*/
public class History implements Book {
@Override
public String name() {
return "历史";
}
}

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("录入的分数有毛病!");
}
}
}

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

View File

@ -0,0 +1,38 @@
package test.java;
/**
*
*/
public class Java {
/**
*
*/
private String name1;
private String name2;
/**
* @param name
* @param name2
*/
public Java(String name, String name2) {
this.name1 = name1;
this.name2 = name2;
}
/**
* main
*/
public void java() {
System.out.println(name1 + "cc");
}
public void sha() {
System.out.println(name2 + "xx");
}
}

View File

@ -0,0 +1,23 @@
import java.util.ArrayList;
import java.util.List;
/**
* ListToArray
*/
public class ListToArray {
/**
*
* @param args
*/
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
/**
*
*/
String[] array = list.toArray(new String[0]);
System.out.println("数组内容: " + list.toString());
}
}

View File

@ -0,0 +1,10 @@
/**
* Book
*/
public class Literature implements Book {
@Override
public String name() {
return "文学";
}
}

View File

@ -0,0 +1,30 @@
//字符与字符串查找
public class New{
public static void main(String args[]){
String str = "How qi bocome handsome like qi ge"; //定义一个长字符串
System.out.println("该字符串为:"+str);
/***1、indexOf()方法查找字符首个出现位置格式1,2***/
int index1 = str.indexOf(" "); //找到第一个空格所在的索引
int index2 = str.indexOf(" ",4); //找到索引4以后的第一个空格所在索引
System.out.println("第一个空格所在索引为:"+index1);
System.out.println("索引4以后的第一个空格所在索引为"+index2);
System.out.println("*****************");
/***2、lastIndexOf()方法查找字符最后出现位置格式1,2***/
int index3 = str.lastIndexOf(" "); //找到最后一个空格所在的索引
int index4 = str.lastIndexOf(" ",10);//找到索引10以后的第一个空格所在索引
System.out.println("最后一个空格所在索引为:"+index3);
System.out.println("索引10以前最后一个空格所在索引为"+index4);
System.out.println("*****************");
/***3、indexOf()方法查找子字符串第一次出现位置格式1,2***/
int index5 = str.indexOf("qi"); //找到"qi"子字符串第一次出现位置的索引
int index6 = str.indexOf("qi",5);//找到索引5以后子字符串"qi"第一个出现位置所在索引
System.out.println("子字符串qi第一次出现位置的索引号为"+index5);
System.out.println("索引5以后子字符串qi第一次出现位置的索引号为"+index6);
System.out.println("*****************");
/***4、lastIndexOf()方法查找子字符串最后一次出现位置格式1,2***/
int index7 = str.lastIndexOf("qi");
int index8 = str.lastIndexOf("qi",5);
System.out.println("子字符串qi最后一次出现位置的索引号为"+index7);
System.out.println("索引号5以后子字符串qi最后一次出现位置的索引号为"+index8);
}
}

View File

@ -0,0 +1,36 @@
public class NewStudent {
/**
*
*/
private String name;
/**
*
*/
private String age;//定义
/**
*
* @param inputName
* @param inputAge
*/
public NewStudent(String inputName,String inputAge){
this.name = inputName;
this.age = inputAge;
}
public void eat(){
System.out.println("吃饭");
}
/**
*
* @param food
*/
public void eat(String food){
System.out.println("学生吃的"+food);
}
public String eat(String food,String time){
System.out.println("学生在" + time + "吃的" + food);
return"吃饱了";//定义吃饭的方法
}
}

View File

@ -0,0 +1,6 @@
/**
*
*/
public interface Shape {
String name();
}

View File

@ -0,0 +1,9 @@
/**
* Shape
*/
public class Square implements Shape {
@Override
public String name() {
return "正方形";
}
}

View File

@ -0,0 +1,107 @@
/**
* @ClassName: StringBufferDemo1
* @Description: StringBuffer
* 线StringBuffer
* StringBuffer synchronized线
* StringBufferStringBuilder
* @author: Zh
* @date: 2024/4/8 16:56
*/
public class StringBufferDemo1 {
public static void main(String[] args) {
/**
* StringBuffer
*/
StringBuffer sb = new StringBuffer("孙悟空");
/**
*
*/
sb.append("会龟派气功");
/**
* StringBuffer
*/
System.out.println("StringBuffer对象" + sb);
/**
* 0
*/
/**
* 0 1 2 3 4 5 6 7
*/
/**
* 2 3
*/
sb.delete(2, 4);
/**
* delete
*/
System.out.println("delete删除" + sb);
/**
*
*/
sb.insert(2, "123");
System.out.println("insert新增" + sb); // insert新增孙悟123龟派气功
/**
*
*/
sb.reverse();
/**
* reverse321
*/
System.out.println("reverse反转" + sb);
/**
* StringBufferString
*/
String s = sb.toString();
/**
* String321
*/
check(s); //
}
/**
* String
*
* @param abc
*/
public static void check(String abc) {
System.out.println("String类型" + abc);
}
}

View File

@ -0,0 +1,123 @@
/**
* @ClassName: StringBuilderDemo1
* @Description: StringBuilder
* 线StringBuilder
* StringBuilder synchronized线
* StringBufferStringBuilder
* @author: Zh
* @date: 2024/4/8 16:41
*/
public class StringBuilderDemo1 {
public static void main(String[] args) {
// StringBuilder的字符串拼接.append
StringBuilder sb = new StringBuilder();
/**
*
*/
sb.append("a");
/**
*
*/
sb.append(1);
/**
*
*/
sb.append(false);
/**
*
*/
sb.append(3.3);
/**
*
*/
sb.append("abc");
/**
* appenda1false3.3abc
*/
System.out.println("append的应用" + sb);
StringBuilder sb1 = new StringBuilder();
/**
*
*/
sb1.append("a").append("b").append("c").append("我爱你中国");
/**
* appendabc
*/
System.out.println("append支持链式编程" + sb1);
/**
* reverse
*/
sb1.reverse();
/**
* reversecba
*/
System.out.println("reverse反转" + sb1);
/**
* length
* length8
*/
System.out.println("字符串长度length" + sb1.length());
/**
* StringBuilder
* String
*/
StringBuilder sb2 = new StringBuilder();
sb2.append("123").append("阿拉伯数字");
/**
* check(sb2); // 报错因为sb2不是String类型
* 123
*/
System.out.println("拼接字符串:" + sb2);
/**
* String
*/
String rs = sb2.toString();
/**
* String123
*/
check(rs);
}
/**
* String
*
* @param abc
*/
public static void check(String abc) {
System.out.println("String类型" + abc);
}
}

View File

@ -0,0 +1,37 @@
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);
}
}

View File

View File

@ -0,0 +1,37 @@
public class TestArray {
/**
*
* @param args
*/
public static void main(String[] args) {
// double类型的数组
double[] scores = new double[]{99.5, 88.0, 75.5};
double[] scores1 = {99.5, 88.0, 75.5}; // 简化写法
// int类型的数组
int[] ages = new int[]{12, 24, 36};
int[] ages1 = {12, 24, 36}; // 简化写法
int size = 5; // 设置数组大小是10
String[] names = new String[size]; // 定义数组
names[0] = "孙悟空";
names[1] = "贝吉塔";
names[2] = "弗利萨";
names[3] = "沙鲁";
names[4] = "魔人布欧";
// 编译不报错运行报错因为数组大小是5这是数组里第6个元素数组越界ArrayIndexOutOfBoundsException
// names[5] = "布尔玛";
// 数组索引从 0 开始,所以索引值从 0 到 arrayRefVar.length-1
for (int i = 0; i < names.length; i++) {
System.out.print("索引:" + i + names[i] + " ");
}
System.out.println();
// 数组变量名存储的是数组在内存种的地址,数组是引用类型
// [ 指的是 数组
// D 指的是 数据类型double
// @ 指的是 在哪里
System.out.println(scores); // [D@776ec8df,指的是数组的十六进制地址
}
}

View File

@ -0,0 +1,22 @@
/**
*
*/
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class TestBook {
public static void main (String[] args) {
List<Book> books = new ArrayList<>();
Book literatureBook = new Literature();
Book historyBook = new History();
books.add(literatureBook);
books.add(historyBook);
for (Book book : books){
System.out.println(book.name());
}
}
}

View File

@ -0,0 +1,22 @@
/**
*
*/
import java.util.ArrayList;
import java.util.List;
/**
*
*/
public class TestShape {
public static void main(String[] args) {
List<Shape> shapes = new ArrayList<>();
Shape circleShape = new Circle(); // 创建Circle
Shape squareShape = new Square(); // 创建Square
shapes.add(circleShape); // 将Circle添加到列表中
shapes.add(squareShape); // 将Square添加到列表中
for (Shape shape : shapes) { // 遍历列表中的每个Shape对象
System.out.println(shape.name()); // 打印
}
}
}

View File

@ -0,0 +1,13 @@
public class TestTop {
public static void main(String[] args){
Top topPerson = new Top("xx","20","2");//创建top对象
topPerson.name();
topPerson.age();
topPerson.ranking();//输出
Top topPer = new Top("cc","21","1");//创建top对象
topPer.name();
topPer.age();
topPer.ranking();//输出
}
}

View File

@ -0,0 +1,75 @@
/**
*
*/
public class Top {
/**
*
*/
private String name;
/**
*
*/
private String age;
/**
*
*/
private String ranking;
/**
*
* @param name
* @param age
* @param ranking
*/
public Top(String name,String age,String ranking){
this.name=name;
this.age=age;
this.ranking=ranking;
}
/**
*
*/
public void name() {
System.out.println("名字: " + name);
}
/**
*
*/
public void age() {
System.out.println("年龄: " + age);
}
/**
*
*/
public void ranking() {
System.out.println("名次: " + ranking);
}
/**
*
*/
public void name1() {
System.out.println("名字: " + name);
}
/**
*
*/
public void age1() {
System.out.println("年龄: " + age);
}
/**
*
*/
public void ranking1() {
System.out.println("名次: " + ranking);
}
}

View File

@ -0,0 +1,12 @@
public class Word {
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");
}
}
}

View File

@ -0,0 +1,11 @@
public class Wount {
public static void main(String[] args) {
String studentId = "20233011082";
char lastDigit1 = studentId.charAt(studentId.length() - 2);
char lastDigit2 = studentId.charAt(studentId.length() - 1);
char[] lastTwoDigits = {lastDigit1, lastDigit2};
for (char c : lastTwoDigits) {
System.out.print(c + " ");
}
}
}

View File

@ -0,0 +1,30 @@
//字符与字符串查找
public class da{
public static void main(String args[]){
String str = "How qi bocome handsome like qi ge"; //定义一个长字符串
System.out.println("该字符串为:"+str);
/***1、indexOf()方法查找字符首个出现位置格式1,2***/
int index1 = str.indexOf(" "); //找到第一个空格所在的索引
int index2 = str.indexOf(" ",4); //找到索引4以后的第一个空格所在索引
System.out.println("第一个空格所在索引为:"+index1);
System.out.println("索引4以后的第一个空格所在索引为"+index2);
System.out.println("*****************");
/***2、lastIndexOf()方法查找字符最后出现位置格式1,2***/
int index3 = str.lastIndexOf(" "); //找到最后一个空格所在的索引
int index4 = str.lastIndexOf(" ",10);//找到索引10以后的第一个空格所在索引
System.out.println("最后一个空格所在索引为:"+index3);
System.out.println("索引10以前最后一个空格所在索引为"+index4);
System.out.println("*****************");
/***3、indexOf()方法查找子字符串第一次出现位置格式1,2***/
int index5 = str.indexOf("qi"); //找到"qi"子字符串第一次出现位置的索引
int index6 = str.indexOf("qi",5);//找到索引5以后子字符串"qi"第一个出现位置所在索引
System.out.println("子字符串qi第一次出现位置的索引号为"+index5);
System.out.println("索引5以后子字符串qi第一次出现位置的索引号为"+index6);
System.out.println("*****************");
/***4、lastIndexOf()方法查找子字符串最后一次出现位置格式1,2***/
int index7 = str.lastIndexOf("qi");
int index8 = str.lastIndexOf("qi",5);
System.out.println("子字符串qi最后一次出现位置的索引号为"+index7);
System.out.println("索引号5以后子字符串qi最后一次出现位置的索引号为"+index8);
}
}