回顾:
1.猜字符游戏:
1)设计数据结构:变量
2)设计程序结构:方法
3)设计算法:方法体
正课:
1.猜字符小游戏:
3)设计算法:
3.1)主方法:
public static void main(String[] args){
}
3.2)生成随机字符数组:
public static char[] generate(){
}
3.3)对比:随机字符数组与用户输入的字符数组
public static int[] check(char[] chs,char[] input){
}
2.总结:
String str = "abc";
1)将字符串转换为字符数组:
char[] input = str.toCharArray();
2)将字符串转换为大写字母:
str = str.toUpperCase();
将字符串转换为小写字母:
str = str.toLowerCase();
3)判断字符串内容是否相等:
if(str.equals("EXIT")){
}
练习:
1)独立完成猜字符游戏
2)运行Shoot射击游戏项目-----大概了解需求
讲项目----------------吃药
讲语法----------------吃糖豆
能够真正的去解决实际问题的人
资深--------------3年(3.5万----14薪)
--------------年薪49万
4年多---------------3.9万(16薪)
---------------年薪62万
6年多---------------2.7万(16薪)
---------------年薪43万
请选择字符的个数(5、7、9):4
请选择字符的个数(5、7、9):8
请选择字符的个数(5、7、9):6
请选择字符的个数(5、7、9):2
请选择字符的个数(5、7、9):7
ABCDEFG
猜吧!
基本类型判断相等-------------用==
String判断内容相等-----------不能用==
用equals()
猜数字-----------1到1000之内的随机数(输入0退出)
随机加法运算器---0到99加法(输出-1退出)
猜字符-----------随机4个字符(输出EXIT退出)
String str = "aBc";
str = str.toUpperCase(); "ABC"
exit时退出
1)手里面藏一个数chs
2)猜吧!接收用户输入的数input
3)判断是否猜对了:
3.1)错了,则提示字符和位置对的个数,重复第2步
3.2)对了,则算分后结束
B:位置对个数为5
C:字符对个数为5,并且,位置对个数为5
String str = scan.next(); "ABC"
char[] input = str.toCharArray();
input[0]='A'
input[1]='B'
input[2]='C'
while(true){ //自造死循环
必须有break
...
...
...
...
}
Math.random()-------------0.0到0.99999999999...
public static char[] genereate() {
char[] chs = new char[5];
char[] letters = {'A','B','C','D',...,'X','Y','Z'};
boolean[] flags = new boolean[letters.length]; //开关数组
for(int i=0;i<chs.length;i++){
do{
int index = (int)(Math.random()*letters.length);
}while(flags[index]==true);
chs[i] = letters[index];
flags[index] = true;
}
i=0 index=0 chs[0]='A' flags[0]=true
i=1 index=25 chs[1]='Z' flags[25]=true
i=2 index=0/25/0/25/1 chs[2]='B' flags[1]=true
i=0 index=0 chs[0]='A'
i=1 index=25 chs[1]='Z'
i=2 index=1 chs[2]='B'
i=3 index=4 chs[3]='E'
i=4 index=23 chs[4]='X'
return chs;
}