java字串,包,數組及空心正方形,菱形的執行個體

來源:互聯網
上載者:User

標籤:for迴圈   寫代碼   第一個   upper   int   --   rgs   stat   資料類型   

一、數組:相同類型的多個對像
參考型別:所有的類,介面,數組,
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字串,包,數組及空心正方形,菱形的執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.