条件运算符
package operater;
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b; //a = a+b
System.out.println(a); //30
}
}
package operater;
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a += b; //a = a+b
System.out.println(a); //30
// 字符串连接符
System.out.println("" + a + b); //3020 字符串在前,变量就会转为字符串进行打印
System.out.println(a + b + ""); //50 变量在前,就正常走计算逻辑
}
}
?:的意思解释
package operater;
public class Demo08 {
public static void main(String[] args) {
// x ? y : z
int souce = 90;
String type = souce > 60 ? "合格" : "不合格"; // 如果大于60为合格,反之为不合格
System.out.println(type);
}
}
打包生成文档
1、cmd生成方式
javadoc -encoding UTF-8 -charset UTF-8 Demo09.java
-encoding UTF-8 -charset UTF-8识别中文时,不乱码
2、IDEA生成javadoc文档
效果如下:
scanner键盘交互
next()
package scanner;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用next的接受");
//判断用户有没有输入数据
if (scanner.hasNext()) {
String str = scanner.next(); // 无法接受带空格的数据,会将空格后的数据屏蔽
System.out.println(str);
}
// 关闭scanner
scanner.close();
}
}
nextline()
package scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数据>>:");
if (scanner.hasNext()) {
String str = scanner.nextLine(); // 会接受所有数据
System.out.println(str);
}
scanner.close();
}
}
案例:输入多个数字回车确认,当输入非数字时,求和及平均值
package scanner;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
// 和
double sum = 0;
// 平均数
int i = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数字以回车键更换,文字结束");
while (scanner.hasNextDouble()) {
double x = scanner.nextDouble();
i += 1;
sum = sum + x;
}
System.out.println(sum);
System.out.println(sum / (double) i);
scanner.close();
}
}