码迷,mamicode.com
首页 > 编程语言 > 详细

java字符串,包,数组及空心正方形,菱形的实例

时间:2017-07-15 23:01:20      阅读:420      评论:0      收藏:0      [点我收藏+]

标签: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 = "12345678@qq.com";

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 = "12345678@qq.com";
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字符串,包,数组及空心正方形,菱形的实例

标签:for循环   写代码   第一个   upper   int   --   rgs   stat   数据类型   

原文地址:http://www.cnblogs.com/NCL--/p/7186421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!