目录
需求:
解决方案:
学习内容:
LintCode刷题:
·拼接两个字符串
1、直接使用"+"来算
2、使用StringBuffer.append方法
3、使用String.concat()方法
三者区别:
concat()方法:String类的concat()方法(只能用于拼接字符串,不能拼接其他类型的数据)将指定的字符串拼接到该字符串的末尾。并且字符串本身和拼接的字符串都不能为null,否则运行程序后会报空指针异常NullPointerException(编译时没有报错)。
"+":可以对字符,数字,字符串等数据类型的拼接
append()方法:可以对字符,数字,字符串等数据类型的拼接,结果返回一个StringBuffer类型的对象
·计算数组的和
for循环建立数组,定义一个存储累加结果的数,在for循环中将下次所输入的值直接加上,可以不用初始化
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int[] arr=new int[n];
int sum=0;
for (int i=0;i<n;i++){
sum+=scanner.nextInt();
}
System.out.println(sum);
}
}
·计算平方根
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
System.out.printf("%.2f\n",Math.sqrt(n));
}
}
·打印工作内容
继承抽象类的类重写抽象类方法
public abstract class People {
public abstract void working();
}
public class Police extends People {
@Override
public void working() {
System.out.println("The work of the police is to catch thieves.");
}
}
public class Chef extends People {
@Override
public void working() {
System.out.println("The work of the Chef is to cook.");
}
}
public class Main {
public static void main(String[] args) {
Chef chef=new Chef();
Police police=new Police();
police.working();
chef.working();
}
}
·实现计算面积方法
继承抽象类
public abstract class Shape {
public abstract double area();
}
public class Square extends Shape {
public int sideLength;
public Square(int sideLength) {
this.sideLength = sideLength;
}
@Override
public double area() {
return sideLength*sideLength;
}
}
public class Circle extends Shape {
public int radius;
public Circle(int radius) {
this.radius = radius;
}
@Override
public double area() {
return radius*radius*Math.PI;
}
}
·三数之中的最大值
Math.max比较即可
public class Solution {
public int maxOfThreeNumbers(int num1, int num2, int num3) {
int max=Math.max(num1,num2);
int resultmax=Math.max(max,num3);
return resultmax;
}
}
学习时间:
2021-8-23 9:00-11:45、13:30-17:00、18:40-21:00
学习产出:
学习博客*1
刷题*6
需求*1