《放愁》 作者:沙家大少
雨湿琵琶半遮羞,窗前月下何人愁?望穿千古,多少愁,点滴在心头!
夕照青山皆映柔,桑前屋后几处秋?纵观世今,多少忧?浅笑回眸,满眼是青州!
如果这篇文章对您有帮助,或是觉得小弟诗词天赋还可以,还请求点个赞!
知识无价,劳动不易,欢迎打赏。
感谢!
输出并换行:System.out.println();
输出但不换行:System.out.print();
import java.util.Scanner; // 导入java.util.Scanner,import用来导入某个类,必须放在java源代码的开头
public class Input {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // 创建Scanner并传入System.in
System.out.print("Input your name: ");
String name = scanner.nextLine(); // 使用scanner.nextLine()读字符串
System.out.print("Input your age: ");
int age = scanner.nextInt(); // 使用scanner.nextInt()读整数
System.out.println("Hi, " + name + ", you are " + age);
}
}
格式化输出:System.out.printf(); 不会自动换行
使用占位符:%xxx,因为计算机表示的数据不一定适合人的阅读
常用的格式化输出占位符:
占位符 | 含义 |
---|---|
%d | 整数 |
%x | 16进制整数 |
%f | 浮点数 |
%s | 字符串,%n$s代表第n个字符串参数 |
%% | %字符本身 |
查询所有的占位符链接
public class Output {
public static void main(String[] args) {
double d = 3.1415926;
System.out.println(d); // 直接打印
System.out.printf("PI = %.2f\n", d); // 打印保留两位小数的浮点数
System.out.printf("PI = %7.2f\n", d); // 打印总长度为7位,保留两位小数的浮点数,长度不够,前面补空格
// 格式化小数:
double f = 0.123456;
System.out.printf("%f\n", f); // 直接打印浮点数
System.out.printf("%e\n", f); // 科学计数法打印浮点数
System.out.printf("%.2f\n", f); // 打印保留两位小数的浮点数
System.out.printf("%6.2f\n", f); // 打印总长度为6位,保留两位小数的浮点数,长度不够,前面补空格
System.out.printf("%+.2f\n", f); // 打印出符号位正号+,及两位小数的浮点数
// 调整参数顺序:
System.out.printf("%s %s %s\n", "A", "B", "C"); // 字符串占位符
System.out.printf("%2$s %1$s %1$s %3$s\n", "A", "B", "C"); // %n$s代表第n个字符串参数
// 参数不够报错,我们可以多传入参数,但不能少传入参数
System.out.printf("%s %s %s\n", "A", "B");
}
}
输出:System.out.println()/print()/printf()
输入:Scanner scanner = new Scanner(System.in); scanner.nextLine()/nextInt()/nextDouble()/…
eclipse快速导入技巧:将鼠标移至报红处,或者在报红处使用快捷键Alt+?,可以获取提示,选择其中一个解决方案
使用if(条件) {…}
根据计算结果(true/false)决定是否执行语句块
public class Hello {
public static void main(String[] args) {
int n = 70;
if(n >= 60) {
System.out.println("及格了");
}
System.out.println("END");
}
}
语句块可以包含多条语句,在条件成立的时候,所有语句都会依次执行
如果只有一条执行语句我们可以省略花括号,但是并不推荐这么做,因为容易赞成阅读错误或者编写错误
public class Hello {
public static void main(String[] args) {
int n = 70;
if(n >= 60)
System.out.println("及格了");
System.out.println("恭喜你"); // 这条语句并不属于if语句块,java程序不依懒于缩进判断语句块
System.out.println("END");
}
}
if 语句块后面还可以跟上else {…},当条件判断为false时执行,else不是必须的
public class Hello {
public static void main(String[] args) {
int n = 70;
if(n >= 60) {
System.out.println("及格了");
} else {
System.out.println("挂科了");
}
System.out.println("END");
}
}
public class IfElse {
public static void main(String[] args) {
int n = 70;
if (n >= 90) {
System.out.println("优秀");
} else if (n >= 60) {
System.out.println("及格了");
} else {
System.out.println("挂科了");
}
}
}
对浮点数进行判断的时候,因为浮点数存在误差,所以用==判断是不靠谱的,我们可以利用浮点数的差值小于某个临界值来判断
public class IfFloat {
public static void main(String[] args) {
double x = 1 - 9.0 / 10;
// FIXME:
if (x == 0.1) {
System.out.println("x is 0.1");
} else {
System.out.println("x is NOT 0.1");
}
System.out.println("x = " + x);
}
}
public class IfFloat {
public static void main(String[] args) {
double x = 1 - 9.0 / 10;
if (Math.abs(x - 0.1) < 0.00001) {
System.out.println("x is 0.1");
} else {
System.out.println("x is NOT 0.1");
}
System.out.println("x = " + x);
}
}
对应用类型判断时,==判断是否指向同一对象,equals()判断内容是否相等
如果变量为null,调用equals()会报错,此时我们可以用短路运算符解决&&,后者将非null的对象放在前面
public class IfString {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO".toLowerCase();
if (s1 == s2) {
System.out.println("s1 == s2");
}
if (s1.equals(s2)) {
System.out.println("s1.equals(s2)");
}
}
}
String s = null;
// Runtime Error: NullPointerException
if(s.equals("hello")) {
System.out.println("yes");
}
String s = null;
if(s != null && s.equals("hello")) {
System.out.println("yes");
}
if("hello".equals(s)) {
System.out.println("yes");
}
if … else可以做条件判断,else可选
不推荐省略花括号{}
多个if … else串联要注意判断顺序
要注意边界条件
要注意浮点数相等判断
引用类型判断相等用equals(),注意避免NullPointerException
根据switch(表达式)跳转到匹配到case结果
继续执行case结果的后续语句
遇到break结束执行
无匹配时执行可选的default
相当于一组 if … else …,注意case语句没有{}
case语句具有穿透性,漏写break将导致意想不到的结果
case除了可以和整形进行比较,还可以和字符串进行比较,比较的是内容相等
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Select 1 ~ 4: ");
int opt = scanner.nextInt();
switch (opt) {
case 1:
System.out.println("Selected 1");
break;
case 2:
case 3:
System.out.println("Selected 2,3");
break;
case 4:
System.out.println("Selected 4");
break;
default:
System.out.println("Not selected.");
}
System.out.println("END");
}
}
如果想要避免漏写break语句,我们可以在eclipse里面点击Windows->Preferences->Java->Compiler->Errors/Warnings
switch语句可以做多重选择
switch的计算结果必须是整形、字符串或枚举类型
千万不要漏写break,建议打开fall-through警告
总是写上default,建议打开missing-default警告
尽量少用switch语句,因为switch通常可以用一些设计模式优化
循环语句就是可以让计算机根据条件做循环计算
条件满足是循环,条件不满足时退出循环
while循环首先判断条件
可能一次都不循环
当循环条件永远满足时变成死循环,死循环导致CPU 100%占用,要避免编写死循环
public class Main {
public static void main(String[] args) {
int sum = 0;
int n = 1;
while (n < 10) {
sum = sum + n;
n++;
}
System.out.println(n); // 10
System.out.println(sum); // 45
}
}
public class Main {
public static void main(String[] args) {
int sum = 0;
int n = 1;
while (n > 0) {
sum = sum + n;
n++;
}
System.out.println(n); // -2147483648
System.out.println(sum);
}
}
while循环先判断循环条件是否满足
while循环可能一次都不执行
编写循环逻辑要小心
do-while先执行循环,再判断条件
条件满足时继续循环
条件不满足时退出
至少循环一次
public class Main {
public static void main(String[] args) {
int sum = 0;
int n = 1;
do {
sum = sum + n;
n++;
} while (n < 10);
System.out.println(n); // 10
System.out.println(sum); // 45
}
}
do-while先执行循环,再判断条件
do-while循环至少执行一次
for循环使用计数器实现循环
计数器变量通常命名为i
for循环要分别设置:计数器初始值,循环前检测条件,每次循环后如何更新计数器
初始化计数器总是被执行
可能循环0次
尽量不要在循环体内修改计数器
计数器变量尽量定义在for循环中
for循环可以缺少初始化条件,循环条件和每次循环更新条件,但通常不推荐这么写,因为很容易变成死循环
用for循环打印数组
public class Main {
public static void main(String[] args) {
int[] ns = { 1, 4, 9, 16, 25 };
for (int i = 0; i < ns.length; i++) {
System.out.println(ns[i]);
}
}
}
public class Main {
public static void main(String[] args) {
int[] ns = { 1, 4, 9, 16, 25 };
for (int i = ns.length - 1; i >= 0; i--) {
System.out.println(ns[i]);
}
}
}
for循环的另一种形式,for each循环
for each循环可以更简单的遍历数组
for each循环能够遍历数组和可迭代数据类型,包括List、Map等
for each循环无法指定遍历顺序
for each循环无法获取数组索引
同时遍历多个数组无法使用for each循环
for(int n : ns) {
System.out.println(ns);
}
int[] ns1 = {1, 2, 3, 4, 5};
int[] ns2 = {0, 0, 0, 0, 0};
for(int i = 0; i < ns1.length(); i++) {
ns2[i] = ns1[i] * ns1[i];
}
for循环通过计数器进行循环
for循环可以遍历数组
最佳实践:计数器变量定义在for循环内部,循环体内部不修改计数器
for each循环可以更简单的遍历数组,在我们不需要索引的情况下,我们总是使用for each循环
循环过程中可以使用break语句跳出循环
break语句跳出的是离它最近的一个for循环
continue语句可以提前结束当前循环,直接继续下次循环
对下标0-5的元素进行求和
public class Break {
public static void main(String[] args) {
int[] ns = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int sum = 0;
for (int i = 0; i < ns.length; i++) {
if (i == 5) {
System.out.println("break");
break;
}
System.out.println("add ns[" + i + "]");
sum = sum + ns[i];
}
System.out.println(sum);
}
}
public class Continue {
public static void main(String[] args) {
int[] ns = { 31, 21, 22, 73, 79, 56, 7, 83, 19, 12, 53, 84, 68 };
int sumOfOdds = 0;
for (int n : ns) {
if (n % 2 == 0) {
System.out.println("skip " + n);
continue;
}
sumOfOdds = sumOfOdds + n;
}
System.out.println(sumOfOdds);
}
}
break语句可以跳出当前循环
break语句通常配合if,在满足条件时提前结束循环
break语句总是跳出最近的一层循环
continue语句可以提前结束本轮循环
continue语句通常配合if,在满足条件时提前结束本轮循环