1. 程式人生 > >java字符串,包,數組及空心正方形,菱形的實例

java字符串,包,數組及空心正方形,菱形的實例

最小 字符串 pla qq郵箱 郵箱地址 一段 代碼 找到 nds

一、數組:相同類型的多個對像
引用類型:所有的類,接口,數組,
int[] ints(變量名) = new int[3]
new:指的是在內存空間重新開辟一塊區域

                String s1 = "abc";
        String s2 = "abc";
        System.out.println(s1==s2);

        String s3 = new String("abc");
        String s4 = new String("abc");
        System.out.println(s3==s4);

        System.
out.println(s3.equals(s4));

分析:

String s1 = "abc";
String s2 = "abc";
System.out.println(s1==s2);//輸出的是true,s1定義之後,s2將會在常量池中尋找所存儲的地址,s1==s2是看是否指向的是同一個內存 存儲地址

String s1 = new String("abc");

String s2 = new String("abc");

System.out.println(s1==s2);//輸出的是false這個則是在常量池中定義一個新的數組,s2也是定義一個新的,就是在常量池中的兩個相同的數組,s1==s2所指向的存儲地址不同,所以是 false

二、包裝類:Integer.parseInt()

int、、、Integer
short、、、Short
long、、、Long
float、、、Float
byte、、、、Byte

    System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Byte.MIN_VALUE);
        System.out.println(Byte.MAX_VALUE);
        System.out.println(Long.MIN_VALUE);
        System.
out.println(Long.MAX_VALUE); System.out.println(Short.MIN_VALUE); System.out.println(Short.MAX_VALUE); System.out.println(Float.MIN_VALUE); System.out.println(Float.MAX_VALUE); System.out.println(Double.MIN_VALUE); System.out.println(Double.MAX_VALUE); System.out.println(Float.parseFloat("12.34"));

分析:分別為各個數據類型的最大最小值

三、字符串處理

String str ="a new world a new strat";
System.out.println(str.length());//返回整個字符串的長度
System.out.println(str.trim());//去掉字符串兩邊的空格的長度
System.out.println(str.trim().length());//返回整個字符串的長度
System.out.println(str.charAt(3));//取出字符串中制定索引位置的字符
System.out.println(str.contains("abc"));//判斷一個字符串是否包含另一個字符串
.startsWith()判斷某個字符串是不是以這個開頭
System.out.println(str.replace(‘a‘,‘b‘));將字符串中的a改為b

.toUpperCase()改變字符串的大小寫(大寫)
.toLowerCase()小寫
.valueOf()將數字轉換成字符串輸出toString()
.indexOf("new")字符串第一次出現的位置
.lastIndexOf("new")最後一次出現的位置,若沒有返回-1
.substring(5)從第5個字符開始截取包括第5個
.substring(5,8)從第一個數字開始截取,到第二個數字結束,不包含第二個數字

str.length();
str.trim();
str.charAt(int i);
str.contains(CharSequence s);
str.startsWith(String s);
str.endsWith(String s);
replace(char o, char n);
replace(CharSequence o, CharSequence n);
split(String s);
toUpperCase();
toLowerCase();
valueOf(any args);
str.indexOf(String s);
str.lastIndexOf(String s);
str.substring(int i);
str.substring(int a, int b);

String str = "像勇士這樣的球隊,只有防守一松懈,他們才能抓住機會,打完了三場,爵士還是沒找到應對勇士的辦法";

1, 寫代碼找出關鍵字"球隊","機會"所在字符串str的索引位置, 找出字符串中第二個"勇士"的位置, 並輸出在控制臺上
2, 定義int型變量m, 取值為第一題中所有索引值的和
3, 在控制臺上輸出m作為char型時顯示的內容
4, 寫代碼實現將str字符串用","分割成數組, 並輸出索引值為4的值
5, 寫代碼實現將str字符串中"爵士"用"勇士"代替, "勇士"用"爵士"代替, 並將結果輸出到控制臺上(禁用replace方法)
6, 編寫代碼從str字符串中取一部分在控制臺上打印這樣一句話: 勇士抓住機會,找到應對辦法
7, 寫一段代碼, 可以取出任意qq郵箱地址中的qq號碼
String qqemail = "[email protected]";

int a=0;
        int b=0;
        int c=0;
        int m=0;
        String str = "像勇士這樣的球隊,只有防守一松懈,他們才能抓住機會,打完了三場,爵士還是沒找到應對勇士的辦法";
        b=str.indexOf("球隊");
        a=str.indexOf("機會");
        c=str.lastIndexOf("勇士");
        m=a+b+c;
        System.out.println((char)m);

技術分享技術分享

System.out.println(str.split(",")[4]);
        String[] newstr = str.split("");
        for(int i=0;i<newstr.length;i++){
            if("".equals(newstr[i])){
                newstr[i]="";
            }else if("".equals(newstr[i])){
                newstr[i]="";
            }
        System.out.print(newstr[i]);
        }

技術分享

System.out.println(str.substring(1,3));
        System.out.println(str.substring(21,26));
        System.out.println(str.substring(37,41));
        System.out.println(str.substring(44,46));

技術分享

String qqemail = "[email protected]";
System.out.println(qqemail.substring(0,8));

技術分享

8, 使用for和if打印一個空心正方形
9, 使用for循環打印一個菱形
10, 使用for循環打印一個空心菱形(選做題)

package aaa;

public class Text {

    public static void main(String[] args) {
        int n = 5;

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                if(i==0||i==4) {
                    System.out.print("* ");
                } else {
                    if(j==0||j==4) {
                        System.out.print("* ");
                    } else {
                        System.out.print("  ");
                    }
                }
            }
            System.out.println();
        }
    }
}

技術分享

    for( int i=4; i>0; i--){  
            for( int j=0;j<8;j++){    
                if(j>i&&j<8-i){
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }
                }System.out.println();
            }
        for(int i=0;i<4;i++){
            for( int j=0;j<8;j++){
            if( j >i&&j<8-i){
                System.out.print("*");
            }else{
                System.out.print(" ");
            }
            }System.out.println();
        }

技術分享

分析:首先確定的是以長寬都為7的正方形,分為兩個部分,上半部分是i表示的是看不到的空格部分,隨著*的增多,i越小,所以為自減,j則為星號的個數自增,下半部分則是i隨著j增加,i逐漸增大,星號的個數也越來越少。

java字符串,包,數組及空心正方形,菱形的實例