您的位置:首页 > 职场人生

java程序员从笨鸟到菜鸟之(十二)String练习

2017-10-31 12:09 253 查看
实例1:

package org.westos.string_03;

/**
*
* @author 王志剑
* 练习题:
*/
public class StringDemo2 {

public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2);               //(1)false
/* 上述代码的反编译查看:
* System.out.println(s3 == (new StringBuilder(String.valueOf(s1))).append(s2).toString());
* 说明:s1+s2执行====>new String("helloworld")
* */
System.out.println(s3.equals((s1 + s2)));        //(2)true
System.out.println(s3 == "hello" + "world");     //(3)true
System.out.println(s3.equals("hello" + "world"));//(4)true
/**
* 通过反编译工具查看第三个输出语句:
* System.out.println(s3 == "helloworld");
System.out.println(s3.equals("helloworld"));
* */
}
}
实例2:(猜数字游戏)
package org.westos.string_03;

import java.util.Scanner;

//提供猜数字游戏的类
public class GuessNumberGame {

//构造私有,通过静态功能
private GuessNumberGame(){

}

//静态功能
public static void start(){
//生成一个随机数:Math.random() ;
int number = (int) (Math.random()*100 +1) ;

//定义一个统计变量
int count = 0 ;
//由于多次录入
while(true){
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
System.out.println("请您输入一个数据:");
int guessNumber = sc.nextInt() ;

//统计变量++
count ++ ;

//判断
if(guessNumber > number){
System.out.println("您猜的数据"+guessNumber+"大了");
}else if(guessNumber < number){
System.out.println("您猜的数据"+guessNumber+"小了");
}else {
System.out.println("恭喜您"+count+"次猜中了...");
break ;
}
}
}
}
实例3:模拟用户登录----玩猜数字游戏

package org.westos.string_03;

import java.util.Scanner;

/**
* 需求:模拟用户登陆,给3次机会,并给提示
*
* 	分析:
* 	       假设:
*      1)定义一个用户和密码
* 		String name = "admin" ;
* 		String password = "admin" ;
* 		2)创建键盘录入对象,录入用户名和密码
* 		3)给3次机会,使用for循环进行操作for(int x = 0 ; x <3 ; x ++){}
*
* 		录入:录入的用户和密码来和已经存在的用户名和密码进行比较,
* 			判断:如果一致:
* 				登陆成功
* 			不一致:
* 				有一个不符合,就登陆不成功
* 				if((2-x)==0){
* 				}else{
* 					//输出还有(2-x)次机会
* 				}
* */
public class StringTest {

public static void main(String[] args) {

//定义用户名和密码
String name = "admin" ;
String password = "admin" ;

//给3次机会
for(int x = 0 ; x <3 ; x ++){
//创建键盘录入对象
Scanner sc = new Scanner(System.in) ;
System.out.println("请输入用户名: ");
String newUserName = sc.nextLine() ;
System.out.println("请输入密码:");
String newPassword = sc.nextLine() ;

//判断
if(name.equals(newUserName) && password.equals(newPassword)){
//一致了
System.out.println("登陆成功,开始玩游戏....");
//加入猜数字游戏
GuessNumberGame.start() ;
break ;
}else{
//登陆不成功
//2,1,0
//如果是0次机会了,换一种提示
if((2-x)==0){
System.out.println("速与管理员联系...");
}else{
//不是0次
System.out.println("你还有:"+(2-x)+"次机会");
}
}
}
}
}

补充:后续会添加验证码?(仅仅先做个数字的验证码)

思路:用户一旦密码输入错误,再登录要输入验证码(Random随机数生成),验证码给用户显示出来,只有账户和密码以及验证码都输入正确才会进入游戏界面(玩猜数字游戏),这只是一个简单的逻辑判断

深入:如果输出汉字呢?是不是得有数据库(后续验证)

说到这里,其实又想到了二维码,二维码是如何制作的呢?有时间了再看看吧!!!

实例4:字符串遍历

package org.westos.string_04;
/**
* 字符串的遍历
*
* 		String str = "helloworld" ;
*
*
* */
public class StringTest {

public static void main(String[] args) {

//定义字符串
String s = "helloworld" ;

/* 由于代码的重复度高,所以使用for循环
* 使用String类的length()和charAt()相结合
*/
for(int x = 0 ; x < s.length() ; x ++){
System.out.print(s.charAt(x)+" ");
}
}
}
实例5:按照指定个格式拼接成一个字符串

package org.westos.string_04;
/**
* 把数组中的数据按照指定个格式拼接成一个字符串举例:
* 		int[] arr = {1,2,3};	输出结果:[1, 2, 3]
*
* 分析:
* 		1)定义数组:int[] arr = {1, 2, 3}
* 		2)定义空字符串:String s = "" ;
* 		3)用空串拼接一个"["
* 		4)遍历int类型的数组,获取到每一个数组中的元素
* 			判断当前某一个索引是否是最大索引
* 			如果是:用空串+= arr[x] ;
* 				   用空串 +="]";
* 			不是:
* 				   用空串+= arr[x]
* 				   用空串+= ", "
*
* @author Apple
*/
public class StringTest2 {

public static void main(String[] args) {
//1)定义一个数组,静态初始化
int[] arr = {1, 2, 3} ;

//将这个数组转换成字符串最终来封装成功能
String result = arrayToString(arr);//调用方法

//直接输出
System.out.println("result:"+result);
}

/**
* 两个明确
* 		明确返回值类型:String类型
* 		明确参数类型:int   int[] arr
* */
public static String arrayToString(int[] arr){
//定义一个空字符串
String result = "" ;

//拼接左中括号
result += "[" ;

//遍历数组
for(int x = 0 ; x < arr.length ; x ++){
//判断
if(x==arr.length-1){
result += arr[x] ;
result += "]" ;
}else{
result += arr[x] ;
result += ", " ;
}
}
return result ;
}

}
实例6:使用键盘录入一个字符串:统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)

package org.westos.string_04;

import java.util.Scanner;

/**
* 需求:使用键盘录入一个字符串:统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
*
* 举例:
* 		"Hello123World"
*
* 		大写字母字符:2个
* 		小写字母字符:8个
* 		数字字符:3个
*
* 分析:
* 		1)定义三个统计变量
* 		2)创建键盘录入对象,录入一个字符串
* 		3)使用for循环遍历字符串中的字符
* 		4)
* 			ASCII码表中
* 					'a'			97
* 					'A'			65
* 					'0'			48
* 		遍历的时候获取到字符
* 			ch>='a' && ch<='z'
* 				属于小写字母字符
* 				smallCount++;
* 			ch>='A' && ch<='Z'
* 				属于大写字母字符
* 				bigCount++
* 			ch>='0' && ch<='9'
* 				属于数字字符
* 				numberCount++
*
* 		5)输出
*
* */
public class StringTest3 {

public static void main(String[] args) {

//保存三个统计遍历
int bigCount = 0 ;
int smallCount = 0 ;
int numberCount = 0 ;

//创建键盘录入对象
Scanner sc = new Scanner(System.in) ;

//录入并接收
System.out.println("请输入一个字符串:");
String line = sc.nextLine() ;

//遍历字符串
for(int x = 0 ; x <line.length() ; x++){
/*
* 重点:charAt(int index);
* 获取字符串的某个索引的字符
* */
char ch = line.charAt(x);

//对当前的这个字符进行判断
if(ch>='a' && ch<='z'){
//小写字母字符
smallCount ++ ;
}else if(ch>='A' && ch <='Z'){
//大写字母字符
bigCount++ ;
}else if(ch>='0' && ch <='9'){
//数字字符
numberCount ++ ;
}
}

//输出结果
System.out.println("当前字符串中大写字母字符共有:"+bigCount+"个");
System.out.println("当前字符串中小写字母字符共有:"+smallCount+"个");
System.out.println("当前字符串中数字字母字符共有:"+numberCount+"个");
}
}
实例7:递归

package org.westos.string_05;

public class StringDemo {

public static void main(String[] args) {

/* 补充:构造方法不能使用递归
* public StringDemo(){
StringDemo();
}*/

/**
*
*递归:方法嵌套
* Math.max(Math.max(a,b),c);
*
*方法递归:方法调用本身的一种现象
*
*		三个条件:
*			1)需要定义个方法
*			2)方法必须有出口条件
*			3)必须有某一种规律
* public void show(int n){
*
* 		if(n<0){
* 			System.exit(0) ; //让jvm退出,程序结束
* 		}
*
* 		System.out.println(n) ;
* 		show(--n) ;
* }
* */
System.out.println("--------------------");
/*需求:求5的阶乘
5! = 5 * 4 * 3 * 2 * 1 ;
5! = 5 * 4! ;*/
int jc = 1 ;
//循环思想
for(int x = 2 ; x <=5 ; x ++){
jc *= x;
}
System.out.println("5的阶乘是:"+jc);
System.out.println("-------------------------");
//使用递归的思想:
//需要定义个方法
System.out.println("5的阶乘是:"+jieCheng(5));
}
/**
* 明确返回值类型:
* 	int类型
* 参数类型:int类型的值
*
* 2)出口条件:
* if(n==1){
* 	return 1 ;
* }
*
* 3)要有规律
b4c3

* 	if(n!=1){
* 		return n* 方法名(n-1);
* }
* */
public static int jieCheng(int n){

if(n==1){
return 1 ;
}else{
return n* jieCheng(n-1) ; //5 * 4 * 3 * 1 *1 
}
}
}
后续待定:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  练习题
相关文章推荐