2021-07-30 Java练习题目录:
1、现在有字符串:
String s1=" SSSCCsdfgsdAX@$#67 ";
String s2=“sssdCCsdfGSDAX@$#67”;
完成以下操作:【默认的是对 s1操作】
(1):保存在字符数组中;
(2):取得第三个元素值;
(3):打印输出该字符串的每个字符;
(4):截取"sd"该子串;
(5):返回"67"的下标位置
(6):返回"gsd"的最后一个下标位置
(7):判断两个字符串是否相等【两种方法操作】
(8):将两个字符串进行连接为一个字符串,s2在前面,s1在后面
(9):去掉s1前后空格
(10):将s1只要是大写字符全部转为小写
(11):判断最后面的字串是不是“67”
(12):判断s2最前面的字串是不是“sss”
package com.practice;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/30 21:19
*/
public class StringPractice1 {
static String s1 = " SSSCCsdfgsdAX@$#67 ";
static String s2 = "sssdCCsdfGSDAX@$#67";
//(1):保存在字符数组中
String[] str1 = {s1, s2};
public static void main(String[] args) {
//(2):取得第三个元素值;
System.out.println(s1.charAt(2));
//(3):打印输出该字符串的每个字符;
System.out.println(s1);
System.out.println(s2);
//(4):截取"sd"该子串;
System.out.println(s1.substring(6, 8));
// (5):返回"67"的下标位置
System.out.println(s1.indexOf("67"));
// (6):返回"gsd"的最后一个下标位置
System.out.println(s1.lastIndexOf("gsd"));
// (7):判断两个字符串是否相等【两种方法操作】
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
// (8):将两个字符串进行连接为一个字符串,s2在前面,s1在后面
System.out.println(s1 + s2);
System.out.println(s1.concat(s2));
// (9):去掉s1前后空格
System.out.println(s1.trim());
// (10):将s1只要是大写字符全部转为小写
System.out.println(s1.toLowerCase());
// (11):判断最后面的字串是不是“67”
System.out.println(s1.endsWith("67"));
// (12):判断s2最前面的字串是不是“sss”
System.out.println(s1.startsWith("sss"));
}
}
2、字符串解析,现有一字符串,“卡巴斯基#杀毒软件#免费版#俄罗斯#”,解析出每个元素。
package com.practice;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/31 11:06
* <p>
* 字符串解析,现有一字符串,"卡巴斯基#杀毒软件#免费版#俄罗斯#",解析出每个元素。
*/
public class StringPractice2 {
public static void main(String[] args) {
String str = "卡巴斯基#杀毒软件#免费版#俄罗斯#";
for (String n : str.split("#")) {
System.out.println(n);
}
}
}
3、“那车水马龙的人世间,那样地来 那样地去,太匆忙”,第二次出现"那"的位置。
package com.practice;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/31 11:13
* <p>
* "那车水马龙的人世间,那样地来 那样地去,太匆忙",第二次出现"那"的位置。
* <p>
* public class fun3 {
* public static void fun1(String str,String str1,int n){
* int ind=0;
* int result=0;
* int k = 0;
* while(k<n){
* ind=str.indexOf(str1);
* if(ind!=-1){
* str=str.substring(ind+str1.length());
* result+=ind;
* System.out.println(str);
* k++;
* }else {
* break;
* }
* }
* if(n==1){
* System.out.println(result);
* <p>
* } else if(n==k){
* System.out.println(result+str1.length()*(n-1));
* }else {
* System.out.println(-1);
* }
* }
* public static void fun1Test(){
* fun1("那车水马龙的人世间,那样地来 那样地去,太匆忙","那",3);
* }
* public static void main(String[] args) {
* fun1Test();
* }
* }
*/
public class StringPractice3 {
static String str = "那车水马龙的人世间,那样地来 那样地去,太匆忙";
public static void main(String[] args) {
System.out.println(str.indexOf("那", str.indexOf("那") + 1));
}
}
4、有一身份证号,判断此为男还是女,基于此方法,写一个算法,判断一个身份证号为男还是女。
package com.practice;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/31 11:18
* <p>
* 有一身份证号,判断此为男还是女,基于此方法,写一个算法,判断一个身份证号为男还是女。
* public class fun4 {
* public static String fun(String str){
* if(str.charAt(str.length()-1)%2==0){
* return "女";
* }else {return "男";}
* }
* public static void main(String[] args) {
* System.out.println(fun("411327200002170718"));
* }
* }
*/
public class StringPractice4 {
public static String fun(String str) {
if (str.charAt(str.length() - 1) % 2 == 0) {
return "女";
} else {
return "男";
}
}
public static void main(String[] args) {
System.out.println(fun("4113274564364564568"));
}
}
5、如下字符串,01#张三#20-02#李四#30-03#王五#40,解析每个人分数多少。样式如下:
01 张三 20
02 李四 30
03 王五 40。并且计算总分。
package com.practice;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/31 11:18
* <p>
* 如下字符串,01#张三#20-02#李四#30-03#王五#40。。。。。。,解析每个人分数多少。样式如下:
* 01 张三 20
* 02 李四 30
* 03 王五 40。并且计算总分。
* <p>
* public class fun5 {
* public static void main(String[] args) {
* String s="01#张三#20-02#李四#30-03#王五#40";
* s=s.replace('#',' ');
* s=s.replace('-',' ');
* String[] s1=s.split(" ");
* int sum=0;
* for (int i = 0; i <s1.length ; i++) {
* if(i%3==0){
* System.out.println();
* }
* System.out.print(s1[i]+" ");
* <p>
* }
* System.out.println("总成绩:"+(Integer.parseInt(s1[2])+Integer.parseInt(s1[5])+Integer.parseInt(s1[8])));
* }
* }
*/
public class StringPractice5 {
public static void main(String[] args) {
String str = "01#张三#20-02#李四#30-03#王五#40";
String[] arr = str.split("-");
int sum = 0;
for (int i = 0; i < arr.length; i++) {
//分割第二次
String[] subarr = arr[i].split("#");
//因为是确定分数的位置的,所以直接计算总分
sum += Integer.parseInt(subarr[subarr.length - 1]);
//输出三兄弟的数组
arr[i] = arr[i].replace("#", " ");
System.out.println(arr[i]);
}
//输出总分
System.out.println("总成绩为:" + sum);
}
}
6、输入一个字符串,判断有多少个字母?多少个数字字符?多少个汉字字符?
package com.practice;
import java.util.Scanner;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/31 11:19
* <p>
* 输入一个字符串,判断有多少个字母?多少个数字字符?多少个汉字字符?
* <p>
* public class fun7 {
* public static void main(String[] args) {
* Scanner sc=new Scanner(System.in);
* int zimu=0;
* int shuzi=0;
* int hanzi=0;
* String x=sc.nextLine();
* for (int i = 0; i <x.length() ; i++) {
* if(x.charAt(i)<'z'&&x.charAt(i)>'a'||x.charAt(i)<'Z'&&x.charAt(i)>'A'){
* zimu++;
* }
* else if(x.charAt(i)>='0'&&x.charAt(i)<='9'){
* shuzi++;
* }
* else if(x.charAt(i)>=0xb0&&x.charAt(i)<=0xf7){
* hanzi++;
* }
* }
* System.out.println("字母--"+zimu);
* System.out.println("汉字--"+hanzi);
* System.out.println("数字--"+shuzi);
* <p>
* }
*/
public class StringPractice7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要分类的字符串:");
//获取输入字符串
String st = sc.nextLine();
//关闭输入流
sc.close();
//定义三个变量来存储所需要的值
int letter = 0;
int Chicharacter = 0;
int integer = 0;
//使用正则表达式清除字符串中所有字母
letter = st.length() - (st.replaceAll("[A-Za-z]", "")).length();
//清除字符串中所有汉字
Chicharacter = st.length() - (st.replaceAll("[\\u4e00-\\u9fa5]{0,}", "")).length();
//清除字符串中所有的数字
integer = st.length() - (st.replace("[0-9]", "")).length();
System.out.println("这个字符串中字母有" + letter + "\n汉字有:" + Chicharacter + "\n数字有:" + integer);
}
}
7、歌词:请问“朋友”出现了多少次。
朋友啊朋友,你可曾想起了我?
如果你正享受幸福,请你忘记我。
朋友啊朋友,你可曾记起了我?
如果你正承受不幸,请你告诉我。
朋友啊朋友。。。
package com.practice;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/31 11:21
* <p>
* 歌词:
* 朋友啊朋友,你可曾想起了我?
* 如果你正享受幸福,请你忘记我。
* 朋友啊朋友,你可曾记起了我?
* 如果你正承受不幸,请你告诉我。
* 朋友啊朋友。。。
* <p>
* 请问“朋友”出现了多少次。
* <p>
* public class fun8 {
* public static int fun1(String str,String str1){
* int ind=0;
* int result=0;
* int k = 0;
* while(true){
* ind=str.indexOf(str1);
* if(ind!=-1){
* str=str.substring(ind+str1.length());
* result+=ind;
* System.out.println(str);
* k++;
* }else {
* break;
* }
* }
* return k;
* <p>
* }
* public static void fun1Test(){
* System.out.println( fun1("朋友啊朋友,你可曾想起了我?\n" +
* "如果你正享受幸福,请你忘记我。\n" +
* "朋友啊朋友,你可曾记起了我?\n" +
* "如果你正承受不幸,请你告诉我。\n" +
* "朋友啊朋友。。。","朋友"));
* <p>
* }
* public static void main(String[] args) {
* fun1Test();
* }
* }
*/
public class StringPractice8 {
public static int str(String str, String sub) {
int s = 0;
for (int i = str.indexOf(sub, 0); i != -1; i = str.indexOf(sub, i + sub.length())) {
s++;
}
return s;
}
public static void main(String[] args) {
System.out.println(str("朋友啊朋友,你可曾想起了我?\n" +
"如果你正享受幸福,请你忘记我。\n" +
"朋友啊朋友,你可曾记起了我?\n" +
"如果你正承受不幸,请你告诉我。\n" +
"朋友啊朋友。。。", "朋友"));
}
}
8、将字符串“我爱中华人民共和国”,变成一个倒叙的字符串。
package com.practice;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/31 11:21
* <p>
* <p>
* 将字符串“我爱中华人民共和国”,变成一个倒叙的字符串。
* <p>
* public class fun9 {
* public static void main(String[] args) {
* String str="我爱中华人民共和国";
* //字符串边字符数字
* char[] s1=str.toCharArray();
* int i=0,j=str.length()-1;
* while (i<j){
* char temp=s1[i];
* s1[i]=s1[j];
* s1[j]=temp;
* i++;
* j--;
* }
* for (char c : s1) {
* System.out.print(c);
* }
* <p>
* }
*/
public class StringPractice9 {
public static void main(String[] args) {
String str = "我爱中华人民共和国";
System.out.println(new StringBuffer(str).reverse().toString());
}
}
9、学生类:学号,姓名,成绩。
重写equals方法:如果学号相同,返回true。
重写toString方法:返回学生的信息,格式“学号-1001;姓名-张三;成绩-90”。
package com.practice;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/31 11:22
* <p>
* 学生类:学号,姓名,成绩。
* 重写equals方法:如果学号相同,返回true。
* 重写toString方法:返回学生的信息,格式“学号-1001;姓名-张三;成绩-90”。
* <p>
* public class Student {
* private int no;
* private String name;
* private int score;
* <p>
* public int getNo() {
* return no;
* }
* <p>
* public void setNo(int no) {
* this.no = no;
* }
* <p>
* public String getName() {
* return name;
* }
* <p>
* public void setName(String name) {
* this.name = name;
* }
* <p>
* public int getScore() {
* return score;
* }
* <p>
* public void setScore(int score) {
* this.score = score;
* }
* @Override public boolean equals(Object o) {
* if (this == o) return true;
* if (o == null || getClass() != o.getClass()) return false;
* Student student = (Student) o;
* return no == student.no;
* }
* @Override public String toString() {
* return "Student{" +
* "学号--" + no +
* ", 姓名--'" + name +
* ", 成绩--" + score +
* '}';
* }
* }
* <p>
* public class fun10 {
* public static void main(String[] args) {
* Student student1=new Student();
* student1.setName("学生1");
* student1.setNo(30);
* student1.setScore(90);
* System.out.println(student1);
* <p>
* Student student2=new Student();
* student2.setScore(89);
* student2.setName("学生2");
* student2.setNo(31);
* System.out.println(student2);
* <p>
* <p>
* System.out.println("判断两个学生是否相等");
* System.out.println(student1.equals(student2));
* }
* <p>
* }
*/
public class StringPractice10 {
public static void main(String[] args) {
Student s1 = new Student(1, "贾乃亮", 99);
Student s2 = new Student(1, "李小璐", 0);
Student s3 = new Student(3, "PGone", 100);
Student s4 = new Student(4, "蔡徐坤", 33);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.print(s1.getName() + "同学和" + s2.getName() + "同学的学号是否相同:");
if (s1.equals(s2)) {
System.out.println("相同");
} else {
System.out.println("不相同");
}
System.out.print(s2.getName() + "同学和" + s3.getName() + "同学的学号是否相同:");
if (s2.equals(s3)) {
System.out.println("相同");
} else {
System.out.println("不相同");
}
System.out.print(s3.getName() + "同学和" + s4.getName() + "同学的学号是否相同:");
if (s3.equals(s4)) {
System.out.println("相同");
} else {
System.out.println("不相同");
}
System.out.print(s4.getName() + "同学和" + s1.getName() + "同学的学号是否相同:");
if (s4.equals(s1)) {
System.out.println("相同");
} else {
System.out.println("不相同");
}
}
}
class Student {
private int ID;
private String name;
private int sc;
public Student() {
}
public Student(int ID, String name, int sc) {
this.ID = ID;
this.name = name;
this.sc = sc;
}
//重写equals方法:如果学号相同,返回true。
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return ID == student.ID;
}
@Override
public String toString() {
return "学号-" + ID +
";姓名-" + name +
";成绩-" + sc;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSc() {
return sc;
}
public void setSc(int sc) {
this.sc = sc;
}
}