第十章----java迴圈練習題

來源:互聯網
上載者:User

標籤:static   stat   pre   ==   print   public   實現   oid   一段   


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

1, 寫代碼找出關鍵字"球隊","機會"所在字串str的索引位置, 找出字串中第二個"勇士"的位置, 並輸出在控制台上
2, 定義int型變數m, 取值為第一題中所有索引值的和
3, 在控制台上輸出m作為char型時顯示的內容
4, 寫代碼實現將str字串用","分割成數組, 並輸出索引值為4的值
5, 寫代碼實現將str字串中"爵士"用"勇士"代替, "勇士"用"爵士"代替, 並將結果輸出到控制台上(禁用replace方法)
6, 編寫代碼從str字串中取一部分在控制台上列印這樣一句話: 勇士抓住機會,找到應對辦法
7, 寫一段代碼, 可以取出任意qq郵箱地址中的qq號碼
8, 使用for和if列印一個空心正方形
9, 使用for迴圈列印一個菱形
10, 使用for迴圈列印一個空心菱形(選做題)

package com.test;import java.util.Arrays;public class lianxi {    public static void main(String[] args) {        String str = "像勇士這樣的球隊,只有防守一鬆懈,他們才能抓住機會,"                + "打完了三場,爵士還是沒找到應對勇士的辦法";        //"球隊","機會"        System.out.println(str.indexOf("球隊"));        System.out.println(str.indexOf("機會"));        System.out.println(str.lastIndexOf("勇士"));                int m = str.indexOf("球隊") + str.indexOf("機會") + str.lastIndexOf("勇士");        System.out.println(m);        System.out.println((char)m);        System.out.println(str.split(",")[4]);                String[] newstr = str.split("");        String temp = "";        for (int i = 0; i < newstr.length; i++) {            if(newstr[i].equals("勇")) {                newstr[i] = "爵";            } else if(newstr[i].equals("爵")) {                newstr[i] = "勇";            }            temp+=newstr[i];        }        System.out.println(temp);        //勇士抓住機會,找到應對辦法        System.out.print(str.substring(str.indexOf("勇士"),str.indexOf("勇士")+2));        System.out.print(str.substring(str.indexOf("抓住機會"),str.indexOf("抓住機會")+4));        System.out.print(str.substring(str.indexOf(","),str.indexOf(",")+1));        System.out.print(str.substring(str.indexOf("找到應對"),str.indexOf("找到應對")+4));        System.out.print(str.substring(str.indexOf("辦法"),str.indexOf("辦法")+2));        System.out.println();                String qqEmail = "[email protected]";        System.out.println(qqEmail.substring(0,qqEmail.indexOf("@")));                        int n = 4;        // 列印空心正方形        for (int i = 0; i < n; i++) {            if(i==0||i==n-1) {                for (int j = 0; j < n; j++) {                    System.out.print("A");                }            } else {                for (int j = 0; j < n; j++) {                    if(j==0||j==n-1) {                        System.out.print("A");                    } else {                        System.out.print(" ");                    }                }            }            System.out.println();        }        System.out.println("列印菱形");        //列印菱形        for(int i=0;i<n;i++) {            for(int j=0;j<n-1-i;j++) {                System.out.print(" ");            }            for(int k=0;k<(2*i+1);k++) {                System.out.print("*");            }            System.out.println();        }        for(int q=0;q<n-1;q++) {            for(int w=0;w<q+1;w++) {                System.out.print(" ");            }            for(int e=0;e<2*n-3-2*q;e++) {                System.out.print("*");            }            System.out.println();        }        System.out.println();        //列印空心菱形        for(int i=0;i<n;i++) {            for(int j=0;j<n-1-i;j++) {                System.out.print(" ");            }            for(int k=0;k<(2*i+1);k++) {                if(k==0||k==2*i) {                    System.out.print("*");                } else {                    System.out.print(" ");                }            }            System.out.println();        }        for(int q=0;q<n-1;q++) {            for(int w=0;w<q+1;w++) {                System.out.print(" ");            }            for(int e=0;e<2*n-3-2*q;e++) {                if(e==0||e==2*n-4-2*q) {                    System.out.print("*");                } else {                    System.out.print(" ");                }            }            System.out.println();        }    }}

也許是這幾天比較熱吧,風扇也沒有修好,熱的腦子都不好使了,這部分題做起來還挺吃力的,思路不熟練。

第十章----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.