String类和Math类练习题

 

1.

String s = "113@ ere qqq yyui"

   请输出所有子串

   113

   ere

   qqq

   yyui

2.

编写一个程序,将下面的一段文本中的各个单词的字母顺序翻转,
“To be or not to be",将变成"oT eb ro ton ot eb."

3.

String s=”name=zhangsan age=18 classNo=090728”;
将上面的字符串拆分,结果如下:
 zhangsan  18  090728

4.

求出当前月份。

package Demo7;

import java.util.Arrays;

public class Demo5 {

	public static void test1() {
		String str = "113@ ere qqq yyui";
		String[] strs = str.split(" ");
		strs[0] = strs[0].substring(0, 3);

		System.out.println("第一题:" + Arrays.toString(strs));
	}

	public static void test2() {
		String str = "To be or not to be";
		String[] strs = str.split(" ");
		for (int i = 0; i < strs.length; i++) {
			char[] ch = strs[i].toCharArray();
			for (int j = 0; j < ch.length / 2; j++) {
				char temp = ch[j];
				ch[j] = ch[ch.length - 1 - j];
				ch[ch.length - 1 - j] = temp;
			}
			strs[i] = String.valueOf(ch);
			if (i == strs.length - 1) {
				strs[i] = strs[i].concat(".");
			}
		}

		System.out.println("第二题:" + Arrays.toString(strs));
	}

	public static void test3() {
		String s = "name=zhangsan age=18 classNo=090728";
		String[] strs = s.split(" ");
		for (int i = 0; i < strs.length; i++) {
			strs[i] = strs[i].substring((strs[i].indexOf("=")) + 1);
		}
		System.out.println("第三题:" + Arrays.toString(strs));
	}

	public static void test4() {
		String str = "2019-07-27";
		String[] strs = str.split("-");
		System.out.println("月份:" + strs[1].substring(1));
	}

	public static void main(String args[]) {
		test1();
		test2();
		test3();
		test4();

	}
}

1. 编写程序将 “jdk” 全部变为大写,并输出到屏幕,截取子串”DK” 并输出到屏幕

2.编写程序将String类型字符串”test” 变为 “tset”.


3.请使用Random或者Math.random生成验证码时随机的6位数字

4.将如下字符串拆分到三个字符串中String
”GET /~minift/epretty/pretty.zip HTTP/1.1”, 拆分后的结果为:
S1 = “GET”
S2 = “/~minift/epretty/pretty.zip”
S3 = “HTTP/1.1”

5.找出"abDjkjoIdasda686KQQ10" 中大写字母 小写字母 非字母个数

6.给定一个字符串str = "yekmaakkccekymbvb"。统计该字符串共多少种字符,并打印出每种字符的个数

 

package Demo7;

public class Demo6 {

	public static void test1() {
		String str = "jdk";
		System.out.println(str.toUpperCase().substring(1));

	}

	public static void test2() {
		String str = "test";
		String[] strs = str.split("");
		String temp = strs[1];
		strs[1] = strs[2];
		strs[2] = temp;
		String str1 = "";
		for (int i = 0; i < strs.length; i++) {
			str1 += strs[i];
		}
		System.out.println(str1);
	}

	public static void test3() {
		int random = 0;
		for (int i = 0, j = 100000; i < 6; i++, j /= 10) {
			random += (int) (Math.random() * 9 + 1) * j;
		}
		System.out.println(random);
	}

	public static void test4() {
		String str = "GET /~minift/epretty/pretty.zip HTTP/1.1";
		String[] strs = str.split(" ");
		for (int i = 0; i < strs.length; i++) {
			System.out.println("s" + (i + 1) + " = " + strs[i]);
		}
	}

	public static void test5() {
		String str = "abDjkjoIdasda686KQQ10";
		char[] ch = str.toCharArray();
		int capital = 0;
		int lowercase = 0;
		int notLetter = 0;
		for (int i = 0; i < ch.length; i++) {
			if (ch[i] >= 65 && ch[i] <= 90) {
				capital++;
			} else if (ch[i] >= 97 && ch[i] <= 122) {
				lowercase++;
			} else {
				notLetter++;
			}
		}
		System.out.println("大写字母个数:" + capital);
		System.out.println("小写字母个数:" + lowercase);
		System.out.println("非字母个数:" + notLetter);
	}

	public static void test6() {
		String str = "yekmaakkccekymbvb";
		char[] ch = str.toCharArray();
		for (char i = 'a'; i <= 'z'; i++) {
			int k = 0;
			for (int j = 0; j < ch.length; j++) {
				if(i == ch[j]) {
					k++;
				}
			}
			if(k != 0) {
				System.out.println(i+"的个数是:"+k);
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("第一题:");
		test1();
		System.out.println("---------------------------");
		System.out.println("第二题:");
		test2();
		System.out.println("---------------------------");
		System.out.println("第三题:");
		test3();
		System.out.println("---------------------------");
		System.out.println("第四题:");
		test4();
		System.out.println("---------------------------");
		System.out.println("第五题:");
		test5();
		System.out.println("---------------------------");
		System.out.println("第六题:");
		test6();
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值