第一关String类
package step1;
import java.util.Scanner;
public class StringLearning {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String stringExample = scanner.next();
String endStr = " end";
// ---------------------Begin------------------------
int len =stringExample.length();
System.out.println("原字符串为:"+stringExample);
System.out.println("字符串长度为:"+len);
System.out.println("转换成大写字符串:"+stringExample.toUpperCase());
System.out.println("转换成小写字符串:"+stringExample.toLowerCase());
System.out.println("第3-7的字符串内容为:"+stringExample.substring(3,7));
System.out.println("字符串相加后:"+stringExample.concat(endStr));
// ---------------------End------------------------
}
}
第2关:StringBuffer类
package step2;
import java.util.Scanner;
public class StringBufferLearning {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String stringExample = scanner.next();
// ---------------------Begin------------------------
StringBuffer sb1=new StringBuffer(stringExample+"educode");
System.out.println("添加数据后:"+sb1);
sb1.delete(5,8);
System.out.println("删除5-8的字符串后:"+sb1);
sb1.replace(7,13,"world");
System.out.println("替换7-13的字符串后:"+sb1);
System.out.println("截取1-10的字符串为:"+sb1.substring(1,10));
sb1.reverse();
System.out.println("反转后的字符串为:"+sb1);
// ---------------------End------------------------
}
}
第3关:Math类
package step3;
import java.util.Scanner;
public class MathClass {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int value1 = scanner.nextInt();
int value2 = scanner.nextInt();
// ---------------------Begin------------------------
/**
1.求出变量value1的绝对值
2.求出value1的3次幂
3.求出value1的二次方根
4.求出value1的sin值
5.求出value1与value2中的较大者
* */
System.out.println("value1="+value1+", "+"value2="+value2);
int n=Math.abs(value1);
System.out.println("value1的绝对值为:"+Math.abs(value1));
double n1=Math.pow(value1,3);
System.out.println("value1的3次幂为:"+String.format("%.6f",n1));
double n2=Math.sqrt(value1);
System.out.println("value1的2次方根为:"+String.format("%.6f",n2));
double n3=Math.sin(value1);
System.out.println("value1的sin值为:"+String.format("%.6f",n3));
System.out.println("value1与value2中的较大者为:"+Math.max(value1,value2));
// ---------------------End------------------------
}
}
第4关:Random类
package step4;
import java.util.Random;
public class RandomClass {
public static void main(String[] args) {
// ---------------------Begin------------------------
Random r = new Random(2022); // 不传入种子
// 随机产生10个[0,100)之间的整数
for (int x = 0; x < 5; x++) {
System.out.println(r.nextInt());
}
for (int x = 0; x < 5; x++) {
System.out.println(r.nextDouble());
}
// ---------------------End------------------------
}
}
第5关:知识回顾
1D 2 正确 3 正确 4StringBuffer