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

一、数组:相同类型的多个对像

引用类型:所有的类,接口,数组,

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(inti);

str.contains(CharSequence s);

str.startsWith(String s);

str.endsWith(String s);

replace(char o, charn);

replace(CharSequence o, CharSequence n);

split(String s);

toUpperCase();

toLowerCase();

valueOf(any args);

str.indexOf(String s);

str.lastIndexOf(String s);

str.substring(inti);

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);

77a8df4e32a4e3bb3bb431e976a3cc18.png

eab10c371a0053d66f9778b2af9a79ef.png

System.out.println(str.split(",")[4]);

String[] newstr= str.split("");for(int i=0;i

newstr[i]="爵";

}else if("爵".equals(newstr[i])){

newstr[i]="勇";

}

System.out.print(newstr[i]);

}

e6d4fcdfc1346e07c0180393d5dc86e5.png

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));

d62d5cfce993ba3b747f447237fb4821.png

String qqemail = "";

System.out.println(qqemail.substring(0,8));

3149f1a183983d34d57d02cebb3b1a2d.png

8, 使用for和if打印一个空心正方形

9, 使用for循环打印一个菱形

10, 使用for循环打印一个空心菱形(选做题)

package aaa;public classText {public static voidmain(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();

}

}

}

cbd94291e70ae4145a60fac88a3cc47c.png

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();

}

3d96969353b8d76a20b5019f7c5cf15d.png

分析:首先确定的是以长宽都为7的正方形,分为两个部分,上半部分是i表示的是看不到的空格部分,随着*的增多,i越小,所以为自减,j则为星号的个数自增,下半部分则是i随着j增加,i逐渐增大,星号的个数也越来越少。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值