文章目录
1.println和scanner的基本用法
输出字符串
Systom.out.println("Hello World!");
使用Scanner来从控制台输入数据
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
练习
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();//36
// 练习scanner输入两个整数求 和\差\积\商
int b = sc.nextInt();//5
System.out.println(a+b);//41
System.out.println(a-b);//31
System.out.println(a*b);//180
System.out.println(a/b);//7
}
}
2.运算符
2.1算数运算符
假设 A=10,B=20
操作符 | 描述 | 例子 |
---|---|---|
+ | 加法 | A+B=30 |
- | 减法 | A-B=-10 |
* | 乘法 | A*B=200 |
/ | 除法 | B/2=2 |
% | 取模 | B%A=0 |
++ | 自增 | B++ 等于21 |
– | 自减 | B-- 等于19 |
练习
public class Main {
public static void main(String[] args) {
int a,b,c;
a = 5;
b = 3;
System.out.println((15*--a)/((c=b--)*(2+9*a)%(b+5)));
}
}
2.2关系运算符
假设 a=10, b = 20
运算符 | 例子 |
---|---|
== | (a==b) False |
!= | (a!=b) True |
“>” | a>b False |
“<” | a<b True |
“>=” | a>=b Flase |
“<=” | a<=b True |
练习
public class Main {
public static void main(String[] args) {
int a=1,b=2;
boolean boo = a<=b;
System.out.println(boo);//true
System.out.println(a++==b);//false
System.out.println(++a==b);//false
System.out.println(a==b--);//false
System.out.println(a==--b);//fasle
System.out.println(a++>=b--);//true
}
}
2.3位运算符
假设a=60;b=13
操作符 | 描述 | 例子 |
---|---|---|
& | 按位与操作符,当且仅当两个操作数的某一位都非0时候的,该位的结果是1 | (a&b),得到12,即00001100 |
| | 按位或操作符,只要两个操作数的某一位有一位非0时候,该位的结果才为1 | (a|b),得到61,即00111101 |
^ | 按位异或操作符,两个操作数的某一位不相同时,该位结果为1 | (a^b)得到49,即00110001 |
~ | 按位反运算符,翻转操作数的每一位 | (~A)得到-60,即11000011 |
<< | 算数左移运算符。左操作数按位左移右操作数指定的位数 | a<<2 得到240,即11110000 |
>> | 算数右移运算符。右操作数按位右移右操作数指定的位数(按符号位填充空位) | -1>>28 还是-1 |
>>> | 逻辑右移补零操作符。左操作数的值按有操作数指定的位数右移,移动得到的空位以零填充。 | -1>>28变为15 |
例子
public class Main {
public static void main(String[] args) {
System.out.println(123&100);//96
System.out.println(34|78);//110
System.out.println(0xAB^88);//243
System.out.println(~100);//-101
System.out.println(10<<3);//80
System.out.println(-137>>3);//-18
System.out.println(-137>>>3);//536870894
}
}
2.4逻辑运算符
假设A = true;B= false
操作符 | 描述 | 例子 |
---|---|---|
&& | 称为逻辑与运算符.当且仅当两个操作数都为真,条件才为为真 | A&&B false |
|| | 称为逻辑或操作符.如果任何两个操作数任何一个为真,条件为真 | A||B true |
! | 逻辑非运算符用来反转操作数的逻辑状态.如果条件为true,则逻辑非运算符将的到false | !(A&&B) true |
例子
public class Main {
public static void main(String[] args) {
int a=1,b=2;
System.out.println(a==b||a==--b);//true
System.out.println(a!=b && a!=b--);//false
System.out.println(!(a++<=b++));//false
System.out.println(!(a==--b)&&a==a);//true
}
}
2.5赋值运算符
操作符 | 描述 | 例子 |
---|---|---|
= | 简单的赋值运算符,将右操作数的值赋给左侧操作数 | |
+= | 加和赋值操作符,把左操作数和右操作数相加赋值给左操作数 | C+=A 等价于C=C+A |
-= | 减和赋值操作符,把左操作数和右操作数相减赋值给左操作数 | C-=A等价于C=C-A |
*= | 乘和赋值操作符,把左操作数和右操作数相乘赋值给左操作数 | C*=A等价于C=C*A |
/= | 除和赋值操作符,把左操作数和右操作数相除赋值给左操作数 | C/=A等价于C=C/A |
%= | 取模和赋值操作符,把左操作数和右操作数取模后赋值给左操作数 | C%=等价于C=C%A |
<<= | 左移位赋值运算符 | C<<=2等价于 C=C<<2 |
>>= | 右移位赋值运算符 | C>>=2 等价于 C=C>>2 |
>>>= | 逻辑右移赋值运算符 | C>>>=2 等价于 C=C>>>2 |
&= | 按位与赋值运算符 | C&=2 等价于 C=C&2 |
^= | 按位异或赋值操作符 | C ^ =2 等价于 C=C^2 |
|= | 按位或赋值操作符 | C|=2 等价于 C=C|2 |
2.6其他运算符
类实例运算符
判断一个对象是否属于某个类(接口)
public class Main {
public static void main(String[] args) {
boolean result = "mike" instanceof String;
System.out.println(result);//true
}
}
3.问号操作符(条件运算符)
条件运算符是唯一的一个三目(三元)运算符
public class Main {
public static void main(String[] args) {
int a = 1;
int b = 2;
int min = a<b?a:b;
System.out.println(min);//1
}
}
public class Main {
public static void main(String[] args) {
int a = 45;
int b = 2;
int c = 3;
int min = a<b?a:b;
System.out.println(min<c? min: c);//2
}
}
4.运算符的优先级
5.条件分支语句
5.1 if语句
public class Main {
public static void main(String[] args) {
// 检查第一个数是否能被第二个数整除
int a = 33;
int b = 3;
boolean boo = false;
if(a%b!=0){
boo = false;
}
else{
boo = true;
}
System.out.println(boo);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 判断体重是标准还是过胖过瘦
int condi = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入体重单位kg:");
int weight = sc.nextInt();
System.out.println("请输入身高cm:");
int height = sc.nextInt();
condi = weight-(height-110);
if(condi>5){
System.out.println("过胖");
}
else if (condi<-5){
System.out.println("过瘦");
}
else{
System.out.println("正常体重");
}
}
}
5.2 switch语句
用例子展示格式
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 使用switch语句分别打印如下结果。
System.out.println("************欢迎使用系统**************");
System.out.println("1.文字录入");
System.out.println("2.记录查询");
System.out.println("3.记录修改");
System.out.println("4.记录删除");
System.out.println("5.推出系统");
System.out.println("*************************************");
System.out.println("请输出要做的操作编号");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
switch (i){
case 1:
System.out.println("文字录入完成!");break;
case 2:
System.out.println("记录查询完毕!");break;
case 3:
System.out.println("记录修改成功!");break;
case 4:
System.out.println("记录删除成功!");break;
default:
System.out.println("退出系统,再见!");
}
}
}