12-操作符和数据类型总结
原创2023年8月21日大约 9 分钟...约 2582 字
0. 目录
- 自增和自减操作符
- 打印 26 个连续的字符
- 找到可以被整除的数
1. 自增和自减操作符
- 自增自减操作符是可以直接改变变量值的操作符
- 前加加和前减减
- 后加加和后减减
++
:其实是一个+1
操作和一个赋值操作的缩写--
:其实是一个-1
操作和一个赋值操作的缩写
public class IncreaseDecrease {
public static void main(String[] args) {
int a = 1;
System.out.println("a++=" + a++); // a++=1
System.out.println("a=" + a); // a=2
/*等价于:
* int a = 1;
* System.out.println("a++=" + a);
* a = a + 1; // or a += 1;
* System.out.println("a=" + a);
* */
int b = 10;
System.out.println("b--=" + b--); // b--=10
System.out.println("b=" + b); // b=9
}
}
输出:
a++=1
a=2
b--=10
b=9
前加加、后加加代码一起来实现一下:
public class IncreaseDecrease2 {
public static void main(String[] args) {
int a = 1;
System.out.println("a++=" + a++); // a++=1
System.out.println("a=" + a); // a=2
a = 1;
System.out.println("++a=" + (++a)); // ++a=2
// System.out.println("++a=" + ++a); // ++a=2
System.out.println("a=" + a); // a=2
int b = 10;
System.out.println("b--=" + b--); // b--=10
System.out.println("b=" + b); // b=9
b = 10;
System.out.println("--b=" + --b); // --b=9
System.out.println("b=" + b); // b=9
}
}
分解动作:
public class IncreaseDecrease3 {
public static void main(String[] args) {
int a = 1;
System.out.println("a++=" + a++);
System.out.println("a++=" + a);
a = a + 1;
a = 1;
System.out.println("++a=" + (++a));
a = a + 1;
System.out.println("++a=" + a);
}
}
2. 打印 26 个连续的字符
2.1 程序中的知识点
- 自动类型转换:char 到 int「
int num = ch;
」 - 强制类型转换:int 到 char 「
(char) num++)
」 - 字符和数字的对应关系,字符集和编码
- 字符串的加法:任何数据和字符串都可以相加,将这个数据的字符串和另一个字符串拼接起来。
- 自增操作符
public class Print26Chars {
public static void main(String[] args) {
char ch = 'A';
int num = ch;
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
}
}
下面这种方法,有风险,还是建议使用下面的最后一行代码。
public class demo {
public static void main(String[] args) {
char ch = 'a';
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println(ch++);
System.out.println((char)(ch++));
}
}
- 如果把
++
放在前面,会怎么样呢? - 如果去掉 int 会怎么样呢?
public class Print26Chars2 {
public static void main(String[] args) {
char ch = 'A';
int num = ch;
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
System.out.println(num + "\t" + (num++));
}
}
输出:
/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=49993:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/apple/Desktop/GitHub/Java_Coder/Coder12/out/production/Coder12 Print26Chars2
65 65
66 66
67 67
68 68
69 69
70 70
71 71
72 72
73 73
74 74
75 75
76 76
77 77
78 78
79 79
80 80
81 81
82 82
83 83
84 84
85 85
86 86
87 87
88 88
89 89
90 90
Process finished with exit code 0
测试 unicode 的代码:
public class Print26Chars3 {
public static void main(String[] args) {
char ch = '\u1234';
// char ch = '\u5234';
int num = ch;
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
System.out.println(num + "\t" + ((char) num++));
}
}
输出:
/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=54697:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/apple/Desktop/GitHub/Java_Coder/Coder12/out/production/Coder12 Print26Chars3
4660 ሴ
4661 ስ
4662 ሶ
4663 ሷ
4664 ሸ
4665 ሹ
4666 ሺ
4667 ሻ
4668 ሼ
4669 ሽ
4670 ሾ
4671 ሿ
4672 ቀ
4673 ቁ
4674 ቂ
4675 ቃ
4676 ቄ
4677 ቅ
4678 ቆ
4679 ቇ
4680 ቈ
4681
4682 ቊ
4683 ቋ
4684 ቌ
4685 ቍ
Process finished with exit code 0
3. 找到可以被整除的数
3.1 程序中的知识点
- 取模运算:整数的取模运算
- 布尔运算:
==
操作符 - 自增运算
public class FindDiv {
public static void main(String[] args) {
int dividend = 35;
int divisor = 9;
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
System.out.println(dividend + "可以整除" + divisor + "吗?" + ((dividend++) % divisor == 0));
}
}
上面写的代码,你会觉得太累了,重复性太强了,就算有快捷复制也会很累的。不要着急,后面会学习到更简便的方法。
欢迎关注我公众号:AI悦创,有更多更好玩的等你发现!
公众号:AI悦创【二维码】
AI悦创·编程一对一
AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh
C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh
方法一:QQ
方法二:微信:Jiabcdefh
你认为这篇文章怎么样?
- 0
- 0
- 0
- 0
- 0
- 0