JavaSE練習—字串、類和物件練習題
一、字串練習題
1.編寫一個程式將一個字串中的字元逆序輸出。abcdefg ----> gfedcba
2.email地址的驗證,要求包含@和”.”。
提示:boolean iscontain = “email_address”.contains(“@”)
3.編寫一個方法用來獲取身份號中的出生日期,方法引數接收身份證號。
public String getBirthday(String idNo){
}
提示:String.substring(…)
4.將字串“123”分別轉換成short、int、long、double型別。
提示:Integer.parseInt(), Integer.valueOf(), Short.parseShort(), ……
5.將字串“2 11 32 44 281”中空格分割的數字取出如:2,11,32,….
提示:用正則表示式: \s+ 表示一個或多個空白符
String.split(regex) //expression “\\s+” --> \s+
6.將下面這段文字中的母音(a,e,i,o,u)的個數分別統計出來,同時統計共有多少個單詞(有空格分開的就算一個單詞):
The String class represents character strings. All string literals in Java programs, such as “abc”, are implemented as instances of this class.
提示
char[] chrs = String.toCharArray()
int a=0,e=0,i=0,o=0,u=0;
for(char chr : chrs){
if(chr==’a’||chr==’A’){
a++;
}else if(chr==’e’||chr==’E’){
e++;
}……
}
7.編寫一個程式實現將以下字串中的資訊統計出來,實現功能如下要求:
字串:microsoft成立於1980年11月22日,CEO是bill gate.
a)統計字串中有多少個英文字母
提示:chrs[] --> chr --> if(chr>=’a’ && chr<=’z’ || chr>=’A’ && chr<=’Z’){ … }
b)統計字串中有多少個數字
提示:
chrs --> chr --> if(chr>=’0’&& chr<=’9’){ … }
c)將字串中的小寫字母轉換成大寫,大寫轉換成小寫。
d)將CEO的名字提取出來
8.編寫一個方法用來驗證郵件地址的合法性,方法引數接收要驗證的郵件地址:
a)要求郵件地地址中包含有“@”和“.”字元,並且“.”字元要在“@”字元的後面。
//email正則表示式
String regex = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
email.matche(regex)
9.將字串:“jjjjjjaaaamesssssss annnnnnnnd jacccccccccccccccckkkkkkkkkk” 中連續出現的字元替換成單個字元。
原始碼:
package com.practice_6;
/**
* @author 阿木木
* @date 2020/12/21 19:24
*/
public class StringPractice {
public static void practice1(String str){ //第一題
char[] chArr = str.toCharArray();
for (int i = chArr.length - 1; i > 0; i--) {
System.out.print(chArr[i]);
}
}
public static void practice2(String str){ //第二題
if (str.contains("@")||str.contains(".")) {
System.out.println("email地址正確");
}
else{
System.out.println("email地址不正確");
}
}
public static void practice3(String idNo){
System.out.println("出生日期:"+idNo.substring(6,10)+"/"+idNo.substring(10,12)+"/"+idNo.substring(12,14));
}
public static void practice4(){
String str = "12345";
System.out.println("short:"+ Short.parseShort(str));
System.out.println("int:"+Integer.parseInt(str));
System.out.println("long:" + Long.parseLong(str));
System.out.println("double:"+Double.parseDouble(str));
}
public static void practice5(String str){
String[] str1 = str.split("\\s+");
for (String s : str1) {
System.out.println(s);
}
}
public static void practice6(){
char[] chrS = ("The String class represents character strings." +
" All string literals in Java programs, such as \"abc\"," +
" are implemented as instances of this class.").toCharArray();
int a=0,e=0,i=0,o=0,u=0;
for(char chr : chrS){
if(chr=='a'||chr=='A'){
a++;
}else if(chr=='e'||chr=='E') {
e++;
}else if (chr=='i'||chr=='I'){
i++;
}else if(chr=='o'||chr=='O'){
o++;
}else if(chr=='u'||chr=='U'){
u++;
}
}
System.out.println(" a:"+a+" e:"+e+" i:"+i+" o:"+o+" u:"+u);
}
public static void practice7(){
String str = "microsoft成立於1980年11月22日,CEO是bill gate.";
char[] chrS = str.toCharArray();
//計數器
int count1 = 0,count2 = 0;
for (char chr : chrS) {
if((chr >='a' && chr<='z') || (chr >='A' && chr <= 'Z')){
count1++;
}else if(chr>='0'&& chr<='9'){
count2 ++;
}
}
System.out.println("字母個數:"+count1);
System.out.println("數字個數:"+count2);
String newStr = str.toUpperCase();
String newStr1 = newStr.toLowerCase();
System.out.println(newStr);
System.out.println(newStr1);
}
public static void practice8(String str){
String regex = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
System.out.println(str.matches(regex));
}
public static void practice9(){
String str = "jjjjjjaaaamesssssss annnnnnnnd jacccccccccccccccckkkkkkkkkk";
System.out.println(str.replaceAll("(.)\\1+", "$1"));
}
public static void main(String[] args) {
practice1("abcdefg");
practice2("[email protected]");
practice3("452231200103154413");
practice4();
practice5("2 11 32 44 281");
practice6();
practice7();
practice8("[email protected]");
practice9();
}
}
二、StringBuffer練習題
10.編寫一個控制檯程式,重複輸5個學生的資訊(學生姓名,年齡,性別),並把這5個學生物件加入到StringBuffer中buffer.append(student),最後輸出StringBuffer的內容,輸出格式如下:
1.張三 22 男
2.李四 18 女
……
注意每個學生的資訊輸出一行。
原始碼:
package com.practice_6;
import java.util.Scanner;
/**
* @author 阿木木
* @date 2020/12/22 19:36
*/
public class StringBufferPractice {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
StringBuffer student = new StringBuffer();
for (int i = 0; i < 2; i++) {
student.append(input.nextLine());
}
System.out.println(student);
}
}
三、類和物件
做練習或者作業時都寫成一個類一個Java檔案
練習1:
請編寫一個Pig 類,可以完成
(1)輸出自己的名字,體重,年齡,顏色
練習2:
練習3:
原始碼:
package com.practice_6;
/**
* @author 阿木木
* @date 2020/12/22 20:06
*/
public class ClassObject {
public static void main(String[] args) {
Pig stu = new Pig();
System.out.println(stu.name);
System.out.println(stu.age);
System.out.println(stu.weight);
System.out.println(stu.color);
}
}
class Student1{//學生類
public String name ;
public int age ;
public String course;
public String hobby;
public void showStudent1(){
System.out.println(this.name);
System.out.println(this.age);
System.out.println(this.course);
System.out.println(this.hobby);
}
}
class Teacher1{//教師類
public String name ;
public String major;
public String teachCourse;
public int teachAge ;
public void showTeacher1(){
System.out.println(this.name);
System.out.println(this.major);
System.out.println(this.teachCourse);
System.out.println(this.teachAge);
}
}
class Pig{
public String name = "石都林";
public int age = 19;
public double weight = 120.0;
public String color = "black";
}
class Dog{
public String sex ;
public String color ;
public int age ;
public double weight ;
public void dogBark(){
}
public void dogEat(){
}
public void dogExcretion(){
}
}
class Music{
public String language ;
public double time;
public String singer;
public void sing(){
}
public void sell(){
}
}