package demo211123;
public class TestA {
// 字符串内容倒置
public static void main(String[] args) {
String t0 = "xxyyzz";
String t1 = "xx" + "yy" + "zz";// String t1 = "xxyyzz";
// 字符串常量拼接--在编译的阶段,优化成一个字符串
System.out.println(t0 == t1);// true
String p0 = "xxyyzz";
String p2 = "xx";
String p1 = p2 + "yy" + "zz";// 字符串变量拼接
System.out.println(p0 == p1);// false
}
public static void main3(String[] args) {
String s0 = new String("ttt");
s0 = s0 + "a\"\\aa" + "bbb" + "ccc";
// a " \ a a 只写入五个字符(\为转义字符)
System.out.println(s0);
StringBuffer s1 = new StringBuffer("abc");// 线程安全
System.out.println(s1.length());
System.out.println(s1.capacity());
StringBuffer s2 = s1.append("aaa").append("bbb").append("bbb");
System.out.println(s1);
System.out.println(s2);
System.out.println(s1.length());
System.out.println(s2.capacity());
System.out.println(s1 == s2);
StringBuilder s9 = new StringBuilder("ttt");// 线程不安全
}
// length长度
// capacity容量--initial capacity of 16 character(or +16)
// 默认容量是16
// 扩容--new capacity = value. length <<1 +2
// 扩大2倍加2
public static void main2(String[] args) {
// 字符串常量池
// 当以String st1="abc";形式创建对象时
// 会首先在字符串常量池中寻找是否存在abc字符串
// 若存在则从字符串池中返回它的地址引用
// 若不存在,则将"abc"添加到字符串池中,然后返回该引用。
String st1 = "abc";
String st2 = "abc";
System.out.println(st1 == st2);// true
String st3 = new String("abc");
System.out.println(st1 == st3);// false
st3 = st3.intern();
System.out.println(st1 == st3);// true
}
public static void main1(String[] args) {
String s1 = null;
String s2 = "";
String s3 = new String();
System.out.println(s1);//null
System.out.println(s2);
System.out.println(s1 == null);// 1=1// true
System.out.println(s1 == "");//false
System.out.println(s2 == "");//true
System.out.println(s3 == "");//false
System.out.println(s2 == s3);//false
System.out.println(s2.equals(s3));//true
System.out.println("abc".equalsIgnoreCase("ABC"));//true
}
// 判空
if ("".equals(s2)) {
}
if (s2 == null || s2.equals("")) {
}
if (s2 != null && !s2.equals("")) {
}
// equals在String中比较的是字符串中的内容是否相同
// equals在默认情况下,比较的是两个对象的地址引用是否相同
// equals是方法,所以是引用类型方法来使用 、
// == 引用类型变量--地址引用是否相同
// == 基本数据类型变量--数值大小是否相同
TestA t1 = new TestA();
TestA t2 = new TestA();
TestA t3 = t1;
System.out.println(t1.equals(t2));// false
System.out.println(t1.equals(t3));// true --t1==t3
}
}
package demo211123;
// 语句
// 表达式
// 如 a-5
// 运算符:
// 算数 +-*/% ++a a--
// 整数相除--取整
// 赋值+= -= *= /= %=
// 比较== != >= <= < >
// 逻辑&& || !
// 条件
// 位 & | ^ ~ >> << >>> // 取余或取模运算过程相近
// 1-求商:c=a/b;
// 2-求模或余: r=a-c*b
// 正负区别
// 取余取=运算结果的符号和被除数一致
// 模=运算结果的符号和除数一致
// 取余和取模在除数和被除数同为正数或负数时没有区别。
// 浮点数区别=在第一步求的方法上是不同的
// 取余运算=在计算c时,向0方向舍入(取整/舍去小数)
// 取模运算=在计算c时,向负无穷方向舍入(类似向下取整
import org.junit.jupiter.api.Test;
public class TestB {
@Test
public static void main(String[] args) {
int a = 5, b = 3;
int c = a + b;
System.out.println(a % b);
System.out.println(7 % 4);
System.out.println((-7) % 4);
System.out.println(7 % (-4));
System.out.println((-7) % (-4));
System.out.println(Math.floorMod(7, 4));
System.out.println(Math.floorMod(-7, 4));
System.out.println(Math.floorMod(7, -4));
System.out.println(Math.floorMod(-7, -4));
}
@Test
public static void main2(String[] args) {
double x = 64.0, y = 0.0;
System.out.println(x / y);// Infinity
System.out.println(x % y);// NaN=not a number
}
@Test
public static void main3(String[] args) {
short a = 1;
a += 1;
System.out.println(a);
short b = 1;
// b = b + 1;
System.out.println(b);
}
@Test
public static void main4(String[] args) {
int a = 5, b = 6;
System.out.println(a != b);// true/false
}
public static void main(String[] args) {
int a = 6, b = 3, c = a + --b, d = a++ + -+b;
System.out.println(c);
System.out.println(d);
}
}
package demo211123;
import java.util.Scanner;
public class TestC {
// 流程控制语句=顺序、分支、循环
// 输入一个数,输出是奇数还是偶数?
static void f4(int i) {
if (i % 2 == 0) {
System.out.println("这个数是偶数");
} else {
System.out.println("这个数是奇数");
}
}
static int f1() {// return
int t = 2;
if (t == 2) {// 分支:条件判断的作用范围
System.out.println("aaa1");
System.out.println("aaa2");
return 10;
}
System.out.println("bbb");
return 20;// 分返回结果+结束方法
}
// if/else格式if(boolean){}else{}
static void f2() {
int t = 2;
if (t == 2) {
System.out.println("aaa");
return;
} else // {
;
System.out.println("bbb");
// } //当省略{}时,分支所能控制的只有一条语句
System.out.println("ccc");
}
void f3() {
int t = 1;
if (t == 1) {
System.out.println(11);
} else if (t == 2) {
System.out.println(22);
} else if (t == 3) {
System.out.println(33);
} else if (t == 4) {
System.out.println(44);
}
System.out.println(55);
}
public static void main(String[] args) {
// System.out.println(111);
// System.out.println(222);
// System.out.println(333);
// System.out.println(555);
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个数");
int i = scan.nextInt();
f4(i);
}
}
package demo211123;
import java.util.Scanner;
import org.junit.jupiter.api.Test;
public class TestD {
Scanner sc = new Scanner(System.in);
@Test
public void t01() {
System.out.println("---------------A");
int a = sc.nextInt();
// String a = sc.nextLine();
//sc.nextLine();
System.out.println(a);
String b = sc.nextLine();
System.out.println(b);
String c = sc.nextLine();
System.out.println(c);
}
// 注解@
// 单元测试
@Test // junit--公有的/非静态的/无参的
public void test() {
Scanner sc = new Scanner(System.in);
String value = sc.nextLine();
int result = Integer.parseInt(value);
System.out.println(result % 2 == 0 ? "偶数" : "奇数");
}
public static void main(String[] args) {
// 1-输入
Scanner sc = new Scanner(System.in);
// int result = sc.nextInt();
String value = sc.nextLine();// 类型转换
int result = Integer.parseInt(value);
// 2-判断
if (result % 2 == 0) {// 3-输出
System.out.println("偶数");
} else {
System.out.println("奇数");
}
}
}
package demo211123;
//流程控制语句
//顺序--包括方法调用
//分支--if/switch
//循环--for/while/do-while
import org.junit.jupiter.api.Test;
import java.util.Scanner;
public class TestE {
// 输入三个数,输出最大值
@Test
public void f52() {
Scanner scan = new Scanner(System.in);
String a = scan.nextLine();
double a1 = Double.parseDouble(a);
String b = scan.nextLine();
double b1 = Double.parseDouble(b);
String c = scan.nextLine();
double c1 = Double.parseDouble(c);
if (a1 > b1 && a1 > c1) {
System.out.println("最大值" + a1);
} else if (b1 > a1 && b1 > c1) {
System.out.println("最大值" + b1);
} else {
System.out.println("最大值" + c1);
}
}
// switch
@Test
public void f51() {
Scanner scan = new Scanner(System.in);
String sr = scan.nextLine();
int result = Integer.parseInt(sr);
switch (result / 10) {
case 10:
System.out.println("A");
break;
case 9:
case 8:
System.out.println("B");
break;
case 7:
System.out.println("C");
break;
case 6:
System.out.println("D");
break;
default:
System.out.println("E");
break;
}
}
public void f5() {
int i = 2;
switch (i) {
case 1:
System.out.println(11);
break;
case 2:
System.out.println(22);
break;
case 3:
System.out.println(33);
break;
default:
System.out.println(99);
}
System.out.println(0);
}
}
方法套方法完成f51()问题
package demo211123;
import java.util.Iterator;
import org.junit.jupiter.api.Test;
public class TestF {
@Test
public void f5() {
for (int m = 1; m <= 5; m++) {
for (int n = 1; n <= m; n++) {
System.out.println(m + " " + n);
}
}
}
@Test
public void f4() {//嵌套
for (int m = 1; m <= 5; m++) {
for (int n = 1; n <= 5; n++) {
System.out.println(m + " " + n);
if (n == 3)
break;
}
}
}
@Test
public void f3() {// 循环是否可以提前结束
for (int k = 0; k < 10; k++) {
if (k == 3) {
continue;// 跳过本次循环之后的带码
}
if (k == 6) {
// break;// 终止所在的这一循环
return;// 返回结果,结束方法
}
System.out.println(k);
}
System.out.println(111);
}
// 输出1-100内偶数的和
@Test
public void f2() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0)
sum += i;
}
System.out.println(sum);
}
// 循环
// for(A初始化;B循环条件;D){C循环条件}
// 循环从什么时候开始到什么时候结束
// 作用范围:循环体
@Test
public void ffor() {
for (int i = 0; i <= 5; i++) {// 循环变量
System.out.println(i);
}
// System.out.println(i);
System.out.println(99);
}
}
while do/while循环
public static void main(String[] args) {
// while(boolean){}
int i = 0;
while (i < 5) {
System.out.println(i++);
}
i = 0;
while (true) {
if (i == 5) {
break;// continue;//return;
}
i++;
}
i = 0;
while (++i < 5) {
System.out.println(i);
}
i=0;
do {
System.out.println(i);
}while(i++<5);
}