一、回顾
1、创建字符串:
1)直接创建并赋值:String s = "";
2)声明的同时引用:String s = new String("wegweh");
2、方法:
1)长度(字符的个数):public int length()
2)比较内容:public boolean equals()
3)比较时忽略大小写:public boolean equalsIgnorecase()
4)获得字符串的大写形式:public String toUpperCase()
5)获得字符串的小写形式:public String toLowerCase()
6)连接:"+" (该操作符的前后如果有字符串类型的操作数,就起到连接作用)
public String concat(String str);
String s="hello ";
s=s.concat("ok");
7)获得自定位置上的字符:
public char charAt(int index);
8)在字符串中查找指定字符或字符串出现的位置:
注意:返回值表示第一次出现的位置,如果没有找到,返回值-1
public int indexOf(char c);
public int indexOf(String str);
public int indexOf(char c,int startIndex);//从指定位置开始
public int indexOf(String str,int startIndex);//从指定位置开始
String s="hello ";
s.indexOf('o'); 4
s.indexOf("he"); 0
s.indexOf("qq"); -1
9)在字符串中查找自定字符或字符串出现(最后出现)的位置
public int lastIndexOf(char c);
public int lastIndexOf(String str);
public int lastIndexOf(char c,int startIndex);//从指定位置开始
public int lastIndexOf(String str,int startIndex);//从指定位置开始
10)在指定字符串中截取字符串:
public String substring(int indexFrom);//从indexFrom开始一直到字符串尾部
public String substring(int indexFrom,int endIndex);
//从indexFrom开始,截到endIndex之前,不包括endIndex(1~>)。
题目1:
编写程序,该程序可以实现对输入的所谓邮箱字符串,进行判断,看是否合法,
邮箱的命名格式如下,里面必须有"@",而且还要有".",点还要在“@”之后,后缀
应该在“com,org,edu”范围内。
题目2:
编写程序,循环录入系统输入的信息,直到录入“q”为止。
二、今天的内容
游戏名称:剪刀(1)、石头(2)、布(3)
可以实现人机对战,游戏过程中,有精彩对白,游戏结束时,有总的统计。
提示:
人出拳可以自我控制,数字在1~3范围内,公平起见,机器出拳随机产生,范围1~3。
随机数字:
Math.random() -->0.0~1.0
((int)Math.random()*10)%3+1;
(0.0,1.0)*10
(int)(0.0,10.0)-->[0,9]%3 -->(0~2)+1 -->1~3
强制类型转换:
(类型)表达式;
(int)2.5=2;
1、创建字符串:
1)直接创建并赋值:String s = "";
2)声明的同时引用:String s = new String("wegweh");
2、方法:
1)长度(字符的个数):public int length()
2)比较内容:public boolean equals()
3)比较时忽略大小写:public boolean equalsIgnorecase()
4)获得字符串的大写形式:public String toUpperCase()
5)获得字符串的小写形式:public String toLowerCase()
6)连接:"+" (该操作符的前后如果有字符串类型的操作数,就起到连接作用)
public String concat(String str);
String s="hello ";
s=s.concat("ok");
7)获得自定位置上的字符:
public char charAt(int index);
8)在字符串中查找指定字符或字符串出现的位置:
注意:返回值表示第一次出现的位置,如果没有找到,返回值-1
public int indexOf(char c);
public int indexOf(String str);
public int indexOf(char c,int startIndex);//从指定位置开始
public int indexOf(String str,int startIndex);//从指定位置开始
String s="hello ";
s.indexOf('o'); 4
s.indexOf("he"); 0
s.indexOf("qq"); -1
9)在字符串中查找自定字符或字符串出现(最后出现)的位置
public int lastIndexOf(char c);
public int lastIndexOf(String str);
public int lastIndexOf(char c,int startIndex);//从指定位置开始
public int lastIndexOf(String str,int startIndex);//从指定位置开始
10)在指定字符串中截取字符串:
public String substring(int indexFrom);//从indexFrom开始一直到字符串尾部
public String substring(int indexFrom,int endIndex);
//从indexFrom开始,截到endIndex之前,不包括endIndex(1~>)。
题目1:
编写程序,该程序可以实现对输入的所谓邮箱字符串,进行判断,看是否合法,
邮箱的命名格式如下,里面必须有"@",而且还要有".",点还要在“@”之后,后缀
应该在“com,org,edu”范围内。
题目2:
编写程序,循环录入系统输入的信息,直到录入“q”为止。
二、今天的内容
游戏名称:剪刀(1)、石头(2)、布(3)
可以实现人机对战,游戏过程中,有精彩对白,游戏结束时,有总的统计。
提示:
人出拳可以自我控制,数字在1~3范围内,公平起见,机器出拳随机产生,范围1~3。
随机数字:
Math.random() -->0.0~1.0
((int)Math.random()*10)%3+1;
(0.0,1.0)*10
(int)(0.0,10.0)-->[0,9]%3 -->(0~2)+1 -->1~3
强制类型转换:
(类型)表达式;
(int)2.5=2;