任务一:

任务描述:

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

方法一:

代码逻辑:

1
2
3
1. 用char数组存储“jdk16”
2. 用for语句和if语句进行大写转换
3. 用数组进行字串输出(也可以转换成字符串之后截取字串输出,但是完全没有必要)

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Two_String_1_1 {
public static void main(String[] args) {
//建立char数组,存储“jdk16”
char[] jdk = new char[5];
jdk[0] = 'j';
jdk[1] = 'd';
jdk[2] = 'k';
jdk[3] = '1';
jdk[4] = '6';
int length = 5;
//大写转换
for(int i = 0; i <= length - 1; i ++) {
if(jdk[i] >= 97 && jdk[i] <= 122) {
jdk[i] -= 32;
}
}
//输出“DK”
System.out.println(jdk[1] + "" + jdk[2]);
}
}

运行检测:

自评:

  • 缺点:

    方法常规,没有特色,

  • 优点:

    容易想到QAQ

收获:

如何连续的输出两个char类型变量

方法二:

代码逻辑:

1
2
3
1. 将jdk16存储到String类型变量中
2. 通过toUpperCase方法将小写字母转换为大写字母。
3. 用substring方法输出子串“DK”

代码实现:

1
2
3
4
5
6
7
8
9
10
public class Two_String_1_2 {
public static void main (String[] args) {
//将jdk16存储到String类型变量中
String str = "jdk16";
//调用toUpperCase方法将小写字母转换为大写字母。
str = str.toUpperCase();
//用substring方法输出子串“DK”
System.out.println(str.substring(1,3));
}
}

代码检测:

自评:

  • 优点:

    代码简洁,规范,效率较高。

  • 缺点:

    不容易想到,需要熟读API文档,或者百度抄答案 -_-

API文档:

任务二:

任务描述:

1
统计任意给定字符串中数字个数。

代码逻辑:

1
2
3
1. 输入字符串
2. 用length方法获取字符串长度,用toCharArray方法将字符串转化为char型数组。
3. 判断是否为数字并输出其个数

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;
public class Two_String_2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//输入字符串
System.out.println("请输入一行字符串:");
String str = scan.nextLine();
scan.close();
//用length方法获取字符串长度,用toCharArray方法将字符串转化为char型数组。
int length = str.length();
char[] chr = new char[length];
chr = str.toCharArray();
//判断每个字符是否是数字并计数
int count = 0;
for(int i = 0; i <= length - 1; i++) {
if(chr[i] >= 48 && chr[i] <=57) {
count ++;
}
}
//输出
System.out.println("您输入的字符串中有" + count + "个数字。");
}
}

运行检测:

自评:

中规中矩,主要是String相关方法的使用。

任务三:

任务描述:

1
2
3
将一个数组从左开始第几位之前的进行旋转:
左旋数组,如:将”abcdef”第2位之前(a为0号位置)
进行旋转后为:”cdefab“。

代码逻辑:

1
2
3
4
5
6
1. 输入字符串和旋转位
2. 将字符串转换为char型数组,并新建一个char型数组存储旋转后的数据。
3. 存储旋转位之前的数组元素
4. 存储旋转位之后的数组元素
5. 将旋转数组转化位字符串
6. 输出旋转字符串

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;

public class Two_String_3 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//输入字符串和旋转位
System.out.println("请输入一个字符串:");
String str = scan.nextLine();
System.out.println("请输入旋转位:");
int location = scan.nextInt();
scan.close();
//将字符串转换为char型数组,并新建一个char型数组存储旋转后的数据。
char[] chr = str.toCharArray();
char[] chr_new = new char[str.length()];
//存储旋转位之前的数组元素
int i = 0;
for(int j = location; j <= str.length() - 1; j++) {
chr_new[i] = chr[j];
i ++;
}
//存储旋转位之后的数组元素
int m = str.length() - location;
for(int n = 0; n <= location - 1; n++ ) {
chr_new[m] = chr[n];
m ++;
}
//将旋转数组转化位字符串
String str_new = new String(chr_new);
//输出旋转字符串
System.out.println("旋转后的字符串为:" + str_new);
}
}

运行检测:

API文档:

自评:

比较满意,

运用的方法比较一般,难点主要在设计上

代码逻辑如果设计好,任务就完成了一大半。

任务四:

任务描述:

1
2
3
将一个数组从左开始第几位之前的进行翻转:翻转数组,如:
将”abcdef”第2位之前(a为0号位置)
进行翻转后为:”bacdef“。

代码逻辑:

1
2
3
4
5
6
1. 输入字符串和翻转位
2. 将字符串转换为char型数组,并新建一个char型数组存储反转后的数据。
3. 存储翻转位之前的数组元素
4. 存储翻转位之后的数组元素
5. 将翻转数组转化位字符串
6. 输出翻转字符串

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;

public class Two_String_4 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//输入字符串和翻转位
System.out.println("请输入一个字符串:");
String str = scan.nextLine();
System.out.println("请输入翻转位:");
int location = scan.nextInt();
scan.close();
//将字符串转换为char型数组,并新建一个char型数组存储翻转后的数据。
char[] chr = str.toCharArray();
char[] chr_new = new char[str.length()];
//存储旋翻转位之前的数组元素
int i = 0;
for(int j = location - 1; j >= 0; j --) {
chr_new[i] = chr[j];
i ++;
}
//存储翻转位之后的数组元素
int m = location;
for(int n = location; n <= str.length() - 1; n++ ) {
chr_new[m] = chr[n];
m ++;
}
//将翻转数组转化位字符串
String str_new = new String(chr_new);
//输出翻转字符串
System.out.println("翻转后的字符串为:" + str_new);
}
}

运行检测:

自评:

和任务三的代码逻辑一样,没有什么难度,任务三的代码稍加修改就是任务四

收获:

Java输出语句中的println、print和printf

任务五:

任务描述:

1
2
3
4
5
编写一个程序,将下面的一段文本中的各个单词的字母顺序翻转,
“To be or not to bo” 将变成
”oT eb ro ton ot ob. ”。(后面加.)

直接来个plus版本的,可将任意输入的文本座椅上变换

代码逻辑:

1
2
3
4
5
6
1. 输入字符串:
2. 使用split将字符串转化为字符串数组并存储到定义的字符串数组中
3. 用for语句将对所有数组元素的翻转处理,转换成对单个数组元素的翻转处理。
4. 对单个字符串的翻转处理:
用substring方法提取子串,然后反向拼接赋值给原字符串数组。
5. 使用print和for语句输出字符串数组,并在最后添加一个“.”

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Scanner;

public class Three_String_5 {
public static void main(String[] args) {
//输入字符串
Scanner scan = new Scanner(System.in);
System.out.println("请输入一句英文(最好不要加标点符号>-<):");
String str = scan.nextLine();
scan.close();
//使用split将字符串转化为字符串数组并存储到定义的字符串数组中
String[] stri = new String[20];
stri = str.split(" ");
//用for语句将对所有数组元素的翻转处理,转换成对单个数组元素的翻转处理。
for(int i = 0; i <= stri.length - 1; i++) {
//用substring方法提取子串
String[] sub = new String[10];
for(int j = 0; j <= stri[i].length() - 1; j ++) {
sub[j] = stri[i].substring(j, j + 1);
}
//反向拼接赋值给原字符串数组
int length = stri[i].length();
stri[i] = "";
for(int k = length - 1; k >= 0; k --) {
stri[i] = stri[i] + sub[k];
}
}
//使用print和for语句输出字符串数组,并在最后添加一个“.”
for(int m = 0; m <= stri.length - 1; m ++) {
if(m < stri.length - 1)
System.out.print(stri[m] + " ");
else
System.out.print(stri[m] + ".");
}
}
}

运行检测:

自评:

难度和三、四差不多,还是先设计代码逻辑,然后搭框架,之后填充代码。

API文档:

任务六:

任务描述:

1
2
字符串转化(压缩)。
如:“aabbccdaa”--------“a2b2c2d1a2”

代码逻辑:

1
2
3
4
5
6
7
1. 输入一串字符串
2. 定义一个StringBuilder变量
3. 用toCharArray方法将字符串转化成char型数组
4. 定义一个char类型的变量来获取字符
5. 定义一个int类型的变量来获取压缩数量
6. 用for语句和contion来让char和int获取正确的数值,并用append方法进行拼接
7. StringBuilder变量转化为String变量输出(不转化直接输出也行)

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Scanner;

public class Three_String_6 {

public static void main(String[] args) {
//输入一串字符串
System.out.println("请输入一串字符串:");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
scan.close();
//定义相关变量
StringBuilder a = new StringBuilder();
char[] b = str.toCharArray();
char char1 = str.charAt(0);
int count = 1;//计数
//用for语句和contion来让char和int获取正确的数值,并用append方法进行拼接
for (int i = 1; i < b.length; i ++) {
if (char1 == str.charAt(i)) {
count++;
continue;
}
a = a.append(char1).append(count);
count = 1;
char1 = str.charAt(i);
}
a = a.append(char1).append(count);
//StringBuilder变量转化为String变量输出
System.out.println("经压缩后为:" + a.toString());
}
}

运行检测:

API文档:

其实append方法有很多个,但都只是参数不同。

自评:

  • 个人认为这个程序相当优秀
  • 我承认这个程序的主体逻辑是我在CSDN上抄的。我原本也写了一个,但是逻辑太过复杂,代码量足足122行,而且最后运行结果还未达到目标,和这个确实比不了,所以也就不拿出来了。

感想:

  1. API文档是个好东西

    背API文档里常用类的方法,或者至少熟读是非常重要的。

    在编程中如果我们知道许多API文档里的方法,原来需要写100多行的代码往往很可能只需要2、30行就能搞定,而且效率一般会更高。

    而且,熟练运用API文档也是面向对象编程的要求之一。

  2. 注释是个好东西

    大家还是要尽量的去给代码写上注释。

    1. 方便:

      很多比较复杂的程序,出现bug是很正常的感觉。如果很长的程序出现了bug,而且还们写注释就会有一种“似曾相识的感觉”,一脸懵逼根本无从下手,根本不敢随意修改。如果加上注释,无论是阅读代码还是修改bug都会方便得多。

    2. 规范代码:

      很多人代码习惯都很一般(比如我),经常不经意间就写了好些垃圾代码。而写注释的过程就是优化代码的过程,很多垃圾代码都会因为没办法添加注释而被修改、删掉。所以说长期写注释对规范代码也有一定作用。

    3. 先设计代码逻辑(框架),再动手写代码

      这个相当重要,很多代码其实写代码并不重要。一些复杂的代码只要代码逻辑设计好,框架搭起来,代码也就完成了一大半。剩下的只需要去无脑的堆代码即可。

      而且,经常设计代码逻辑也有助于拔高视野,能避免在写体量较大的代码时出现走迷宫一样的困境。