键盘录入功能
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年龄");
int age = sc.nextInt();
System.out.println("年龄是" + age);
System.out.println("请输入姓名");
String name = sc.next();
System.out.println("姓名是" + name);
}
}
Random随机数生成
import java.util.Random;
public class test {
public static void main(String[] args) {
Random r = new Random();
int data = r.nextInt(10);//0..9不含10
System.out.println(data);
int data1 = r.nextInt(10) + 5;//5..14
System.out.println(data1);
}
}
数组静态初始化
int[] ages = {12,24,36};
System.out.println(ages[1]);//24
System.out.println(ages.length);//3
double[] scores = {89.9,88.7,99.4};
System.out.println(scores[0]);//89.9
String[] names = {"pete","alxer","bob"};
System.out.println(names[0]);//pete
System.out.println(names.length);//3
数组静态动态初始化
double[] score = new double[3];
System.out.println(score[0]);//0.0
score[0]=11.2;
System.out.println(score[0]);//11.2
String[] names = new String[90];
System.out.println(names[0]);//null
names[0] = "xiang";
System.out.println(names[0]);//xiang
char[] chars = new char[100];
System.out.println(chars[0]);//0
chars[0] = 's';
System.out.println(chars[0]);//s
面向对象
public class car {
String name;
double price;
public void start(){
System.out.println(name + "价格是" + price + "启动");
}
public void run(){
System.out.println(name + "价格是" + price + "跑");
}
}
public class test {
public static void main(String[] args) {
Car c = new Car();
c.name = "奔驰";
c.price = 38.7;
System.out.println(c.name);
System.out.println(c.price);
c.start();
c.run();
Car c1 = new Car();
c1.name = "沃尔沃";
c1.price = 39.7;
System.out.println(c1.name);
System.out.println(c1.price);
c1.start();
c1.run();
}
}
String API
char[] chars = {'a','b','中','国'};
String s3 = new String(chars);
System.out.println(s3);//ab中国
byte[] bytes = {97,98,99,65,66,67};
String s4 = new String(bytes);
System.out.println(s4);//abcABC
String内容比较不能用 ’==‘,’==‘比较的是地址
String内容比较应该用String1.equals(String2) //区分大小写
String内容比较应该用String1.equalsIgnoreCase(String2) //不区分大小写
String name = "我爱中国";
System.out.println(name.length());//获取字符串长度
System.out.println(name.charAt(0));//获取第一个索引字符,我
char[] chars = name.toCharArray();//{'我','爱','中','国'}
String rs = name.substring(0,2);//包前不包后,截取索引[0,1]的字符
System.out.println(rs);//我爱
String rs2 = name.substring(2);//截取当前到最后
System.out.println(rs2);//中国
String rs3 = name.replace("我爱","我喜欢");//替换
System.out.println(rs3);
System.out.println(name.contains("我"));//是否包含我, true
System.out.println(name.contains("你"));//是否包含你, false
System.out.println(name.startsWith("我爱"));//是否以我爱开头, true
String name2 = "张三,李四,王五";
String[] names = name2.split(",");//以逗号分割
System.out.println(names[0]);//张三
System.out.println(names[2]);//王五
ArrayList API
ArrayList list = new ArrayList();
list.add("java");
list.add("is");
list.add("good");
list.add(1);
System.out.println(list);//[java, is, good,1]
list.add(2,"a");
System.out.println(list);//[java, is, a, good,1]
System.out.println(list.get(1));//获取某个索引对应的元素
System.out.println(list.size());//list长度
System.out.println(list.remove(2));//删除第二个元素
System.out.println(list);
System.out.println(list.remove("java"));//删除第二个元素
System.out.println(list);
System.out.println(list.set(0,"java"));//修改元素
System.out.println(list);
ArrayList<String> list1 = new ArrayList();
list1.add("java");
// list1.add(1);//报错
ArrayList<Integer> list2 = new ArrayList();
// list2.add("java");//报错
list2.add(1);
StringBuilder使用
public class StringBuilderDemo1 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(); // ""
sb.append("a");
sb.append("b");
sb.append("c");
sb.append(1);
sb.append(false);
sb.append(3.3);
sb.append("abc");
System.out.println(sb);
StringBuilder sb1 = new StringBuilder();
// 支持链式编程
sb1.append("a").append("b").append("c").append("我爱你中国");
System.out.println(sb1);
// 反转
sb1.reverse().append("110");
System.out.println(sb1);
System.out.println(sb1.length());
// 注意:StringBuilder只是拼接字符串的手段:效率好。
// 最终的目的还是要恢复成String类型。
StringBuilder sb2 = new StringBuilder();
sb2.append("123").append("456");
// 恢复成String类型
String rs = sb2.toString();
check(rs);
}
public static void check(String data){
System.out.println(data);
}
}
Math类
// 1.取绝对值:返回正数
System.out.println(Math.abs(10)); // 10
System.out.println(Math.abs(-10.3)); // 10.3
// 2.向上取整: 5
System.out.println(Math.ceil(4.00000001)); // 5.0
System.out.println(Math.ceil(4.0)); // 4.0
// 3.向下取整:4
System.out.println(Math.floor(4.99999999)); // 4.0
System.out.println(Math.floor(4.0)); // 4.0
// 4.求指数次方
System.out.println(Math.pow(2 , 3)); // 2^3 = 8.0
// 5.四舍五入 10
System.out.println(Math.round(4.49999)); // 4
System.out.println(Math.round(4.500001)); // 5
System.out.println(Math.random()); // 0.0 - 1.0 (包前不包后)
// 拓展: 3 - 9 之间的随机数 (0 - 6) + 3
// [0 - 6] + 3
int data = (int)(Math.random() * 7) + 3;
System.out.println(data);
系统类
System.out.println("程序开始。。。");
// System.exit(0); // JVM终止!
// 2、计算机认为时间有起源:返回1970-1-1 00:00:00 走到此刻的总的毫秒值:时间毫秒值。
long time = System.currentTimeMillis();
System.out.println(time);
long startTime = System.currentTimeMillis();
// 进行时间的计算:性能分析
for (int i = 0; i < 100000; i++) {
System.out.println("输出:" + i);
}
long endTime = System.currentTimeMillis();
System.out.println((endTime - startTime)/1000.0 + "s");
// 3、做数组拷贝(了解)
/**
arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length)
参数一:被拷贝的数组
参数二:从哪个索引位置开始拷贝
参数三:复制的目标数组
参数四:粘贴位置
参数五:拷贝元素的个数
*/
int[] arr1 = {10, 20, 30, 40, 50, 60, 70};
int[] arr2 = new int[6]; // [0, 0, 0, 0, 0, 0] ==> [0, 0, 40, 50, 60, 0]
System.arraycopy(arr1, 3, arr2, 2, 3);
System.out.println(Arrays.toString(arr2));
System.out.println("-------------------");
double i = 10.0;
double j = 3.0;
//
// System.out.println(k1);
System.out.println("程序结束。。。。");
BigDecimal
// 浮点型运算的时候直接+ * / 可能会出现数据失真(精度问题)。
System.out.println(0.09 + 0.01);
System.out.println(1.0 - 0.32);
System.out.println(1.015 * 100);
System.out.println(1.301 / 100);
System.out.println("-------------------------");
double a = 0.1;
double b = 0.2;
double c = a + b;
System.out.println(c);
System.out.println("--------------------------");
// 包装浮点型数据成为大数据对象 BigDeciaml
BigDecimal a1 = BigDecimal.valueOf(a);
BigDecimal b1 = BigDecimal.valueOf(b);
BigDecimal c1 = a1.add(b1);
// BigDecimal c1 = a1.subtract(b1);
// BigDecimal c1 = a1.multiply(b1);
// BigDecimal c1 = a1.divide(b1);
System.out.println(c1);
// 目的:double
double rs = c1.doubleValue();
System.out.println(rs);
// 注意事项:BigDecimal是一定要精度运算的
BigDecimal a11 = BigDecimal.valueOf(10.0);
BigDecimal b11 = BigDecimal.valueOf(3.0);
/**
参数一:除数 参数二:保留小数位数 参数三:舍入模式
*/
BigDecimal c11 = a11.divide(b11, 2, RoundingMode.HALF_UP); // 3.3333333333
System.out.println(c11);