三元运算符
//三元运算符
//x ? y:z x为真结果为y,x为假结果为z
int score = 80;
String type = score>=60 ? "及格":"不及格";
System.out.println(type);
包机制
开发时应把公司的域名倒写,如:www.baidu.com应写成com.baidu.www
文档注释
javadoc命令是为了生成自己的API文件
参数:
-
@author作者
-
@version版本号
-
@since指明需要最早的jdk版本
-
@param参数名
-
@return返回值情况
-
@throws异常抛出状况
cmd命令:javadoc -encoding utf-8 -charset utf-8 Doc.java
Scanner对象
next():
-
一定要读取到有效字符才可以结束
-
对输入有效字符之前遇到的空白,next()方法会自动将其去掉
-
只有输入有效字符才能将其后面输入的空格作为分隔符或结束符
-
next()不能得到带有空白的字符串
//创建一个扫描器对象 用于接受键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有字符串输入
if (scanner.hasNext()==true){
//使用next方式接收
String str=scanner.next();//程序会等待用户的输入完毕
System.out.println("输入的内容为:"+str);
}
//凡是属于IO流的类,用完之后就关掉,节省资源
scanner.close();
nextLine():
-
一Enter为结束符,输出前面的所有字符
-
可以获得空白
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("输入的内容为:"+str);
}
scanner.close();
顺序结构
-
java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一步一步执行
-
顺序结构就是最简单的简单算法
-
语句与语句之间,框与框之间都是按照由上到下的顺序进行的,他是一个任何算法都离不开的一种基本算法结构
选择结构
-
if单选结构
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
//equals判断字符串是否相等
if (s.equals("hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close(); -
if双选结构
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score>=60){
System.out.println("该同学及格,成绩为:"+score);
}else{
System.out.println("该同学不及格,不显示成绩");
}
scanner.close(); -
if多选结构
Scanner scanner=new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if(score<=100 && score>=90){
System.out.println("该学生成绩优异,成绩为:"+score);
}else if(score<90 && score>=80){
System.out.println("该学生成绩良好,成绩为:"+score);
}else if(score<80 && score>=60){
System.out.println("该学生成绩及格,成绩为:"+score);
}else if(score<60 && score>=0){
System.out.println("该学生不及格,不显示成绩");
}else{
System.out.println("该成绩不合法,请重新输入");
}
scanner.close(); -
嵌套的if结构
-
switch多选择结构
-
switch case语句判断一个变量与一系列值中某个值是否相等,给个支称为一个分支
-
switch语句中的变量类型可以是
-
byte、short、int、char
-
从java7开始,switch支持字符串String类型
-
同时case标签必须为字符串常量或字面量
-
-
char grade='C';
//case穿透 switch匹配一个具体的值
switch (grade){
case 'A':
System.out.println("优秀");
break;//可选
case 'B':
System.out.println("良好");
break;//可选
case 'C':
System.out.println("及格");
break;//可选
case 'D':
System.out.println("不及格");
break;//可选
default:
System.out.println("未知等级");
}
循环结构
-
while循环
-
只要布尔表达式为ture,循环就会一直进行
-
大多数情况是会让循环停下来的
-
少部分情况需要一直循环,比如服务器请求响应监听
-
循环条件一直为ture就会造成死循环
-
-
do...while循环
while先判断后执行,d...while先执行后判断
int i=0;
int sum=0;
do {
sum = sum+i;
i++;
}while(i<=100);
System.out.println(sum); -
for循环
for循环是支持迭代的通用结构,是最有效的,最灵活的循环
int a=1; //初始化条件 for(int i=1;i<=100;i++){ System.out.println(i); } System.out.println("for循环结束");
-
九九乘法表
//先打印第一列 //用循环包起来 //去重j<=i //调整样式 for (int i = 1; i <= 9; i++) { for (int j=1;j<=i;j++){ System.out.print(j+"*"+i+"="+(j*i)+" \t"); } System.out.println(); }