Day4-笔记(自增自减运算符、逻辑运算符、位运算、三元运算符、包机制)
自增自减运算符
public static void main(String[] args) {
// 自增++ , 自减-- 一元运算符
int a = 1;
int temp = a++; //先赋值,再+1,运行之后此时a=2了
int temp1 = ++a; //先+1,在赋值,2+1的值赋给右边
System.out.println(temp);
System.out.println(temp1);
int b = 10;
int temp2 = b--; //先赋值,在-1.运行之后此时b=9
int temp3 = --b; //先-1再赋值,此时为9-1的值赋给左边
System.out.println(temp2);
System.out.println(temp3);
//幂运算 ,使用工具类来操作。
double pow = Math.pow(2, 3);
System.out.println(pow);
}
逻辑运算符
&& 与(and) || 或(or) 非! (取反)
public static void main(String[] args) {
boolean A = true;
boolean B = false;
System.out.println("A&&B:" + (B && A)); //与运算,两个都为真则为真,一个为假则为假。
System.out.println("A||B:" + (A || B)); //或运算,有一个为真则为真,
System.out.println("!(A&&B):" + (!(A && B))); //非运算,取反
//短路运算
int c = 10;
System.out.println((c > 10) && (c++ > 10)); //当运行 与运算,第一个取值为False,则不会继续往下计算
System.out.println(c == 10 || c == 11); //当运行 或运算,第一个为True,也不会继续往下计算
}
/*
* 位运算
* A=0011 1100
* B=0000 1101
*
* A&B=0000 1100 两个都为1 才为1,否则为0
* A|B=0011 1101 有一个为1,则为1,否则为0
* A^B=0011 0001 两个位置相同则为0,否则为1
* ~B =1111 0010 取反,取相反的数。0则为1,1则为0
*
*
* 面试题 2*8 怎么做最快 变成2*2*2*2
*
* << 左移 相当于*2
* >> 右移 相当于/2
*
* 0000 0000 0
* 0000 0001 1 左移1位变成2
* 0000 0010 2 左移1位变成4
* 0000 0011 3
* 0000 0100 4 左移1位变成8
* 0000 0101 5
* 0000 0110 6
* 0000 0111 7
* 0000 1000 8 左移1位变成16
* 0000 1001 9
* 0000 1010 10
* 0000 1011 11
* 0000 1100 12
* 0000 1101 13
* 0000 1110 14
* 0000 1111 15
* 0001 0000 16
* */
+=,-=,*=,/= 以及字符串连接符
public static void main(String[] args) {
int a = 10;
int b = 20;
a += b; //a=a+b
System.out.println(a);
a -= b; //a=a-b
System.out.println(a);
//字符串连接符 在运算符两侧,只有出现了String类型,都转换为String并进行连接
System.out.println("aaa" + a + b); //字符串出现在左边,先拼接
System.out.println(a+b+"aaa"); //字符串出现在右边,先运算,再拼接
}
三元运算符
public static void main(String[] args) {
//三元运算符
// x?y:z 如果x==true,则结果为y,否则为z
int score = 50;
String type = score > 60 ? "及格" : "不及格"; //必须掌握
System.out.println(type);
String Life = "起飞"; //人生起飞
String Fate = Life.equals("起飞") ? "走上人生巅峰,发家致富" : "拉闸重来,去他妈的"; //命运
System.out.println(Fate);
}
包机制
package 包的本质-------文件夹
一般利用公司域名倒置作为包名。
例如
com.baidu.www
com.baidu.baike
com.baidu.tieba
用其他类的东西,需要先把这个包导进来
package 放最上面
import放下面
搜 -阿里巴巴开发手册
Java Doc
javadoc命令用来生成API文档的
第一种:通过控制台生成
首先在项目右键
之后打开控制台,在文件路径前面加上cmd即可
之后输入 javadoc -encoding UTF-8 -charset UTF-8 文件名
第二种:在IDEA中可以使用JAVADOC自动生成工具,
JAVA流程控制
用户交互 Scanner
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //system.in是输入,out是输出
System.out.println("使用next方式接受:");
if (sc.hasNext()) {
String str = sc.next();
System.out.println("输入的内容为:" + str);
}
//用完关闭,节省资源
sc.close();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("使用nextline来接收");
if (sc.hasNextLine()) {
String str = sc.nextLine();
System.out.println("输入的内容为:" + str);
}
sc.close();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double sum = 0;
int m = 0;
while (sc.hasNextDouble()) {
System.out.println("请输入第" + (m + 1) + "个数,当前sum=" + sum);
double x = sc.nextDouble();
m++;
sum += x;
}
System.out.println("输入数的和:" + sum);
System.out.println("输入数的平均值:" + sum / m);
sc.close();
}
本文来自博客园,作者:xiaolifc,转载请注明原文链接:https://www.cnblogs.com/xiaolibiji/p/14992625.html
浙公网安备 33010602011771号