概述
这里是昨天未讲完的内容!接上一天!
9.Java的运算符
算术运算符:+ - * / % ++ --
/ 除号右边不能为0
++自增
i++-->i=i+1;
--自减
i--;-->i=i-1;
++i/--i;参与运算时 先自增 再运算
class Demo_1
{
public static void main(String[] arg)
{
int a = 1;
int b = 2;
//先赋值 再运算
int aplus = a++;
int bplus = b--;
System.out.println("aplus "+aplus);
System.out.println("bplus "+bplus);
System.out.println("a---"+a);
System.out.println("b---"+b);
//先运算 再赋值
int aa = ++a;
int bb = --b;
System.out.println("a---"+a);
System.out.println("b---"+b);
System.out.println("aplus "+aa);
System.out.println("bplus "+bb);
}
}
赋值运算符: = += -= *= /= %=
注意:
short b = 2;
b = b+3;//这是两次运算 第一次相加 第二次赋值 不会自动转型
b+=3;//一次运算 自动转换类型
关系运算符:> < <= >= == !=
注意:
关系运算符结果只有 true/flase
逻辑运算符: ! & | ^ && ||
true & true = true
true & false = false
false & ture = false
false & flase = false
只要两边的Boolean表达式结果 有一个为false 结果就是false
两边都为true 结果为true
true | true = true
true | false = false
false | ture = false
false | flase = false
两边只要有一个为true 结果就是true
只有两边为false 结果才是false
true ^ true = false
true ^ false = true
false ^ ture = true
false ^ flase = false
两边相同 结果为false
两边不同 结果为true
&& &的区别
对于:& -- > 不管怎样,都会执行"&"符号左右两边的程序
按位与:a&b是把a和b都转换成二进制数然后再进行与的运算;
对于:&& -- > 只有当符号"&&"左边程序为真(true)后,才会执行符号"&&"右边的程序。
|| |的区别
|两边都要运算
|| 当左边为true 右边就不算了
import java.util.Scanner;
class Demo_3
{
public static void main(String[] args)
{
Test1();
}
public static void Test1()
{
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
System.out.println(age>18 && age<60);
}
public static void Test2()
{
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
System.out.println(age<12 || age>60);
}
public static void Test3()
{
Scanner sc = new Scanner(System.in);
System.out.println("A:输入等于或大于90 但小于100的数");
int a=sc.nextInt();
System.out.println(a>=90 && a<100);
System.out.println("B:输入不是字符q 也不是字符k的字符");
String b = sc.next();
System.out.println(b.charAt(0)!='q' && b.charAt(0)!='k');
System.out.println("C:输入1~9 但不包括5 的数");
int c = sc.nextInt();
System.out.println((c>1 && c<9) && c!=5);
System.out.println("D:输入一个不是1~9的数字");
int d = sc.nextInt();
System.out.println(d<1 && d>9);
}
public static void Tedt4()
{
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
System.out.println(((year % 4 == 0) && year % 100 == 0) && year % 400 == 0);
}
public static void Test5()
{
Scanner sc = new Scanner(System.in);
String s = sc.next();
char c = s.charAt(0);
System.out.println(c>65 && c<90);
}
}
位运算符: & | ^ ~ >> << >>>
<<左移运算符
移动n位就是乘以2的n次幂 右侧补(最高位是什么补什么)
>>右移运算符 带符号右移
移动n位就是除以2的n次幂 左侧补
>>>无符号右移运算符
无论最高位是什么 右移后都补0
& 按位与
0--> false
1--> true
6 & 3 = 2
110
& 011
------
010 -->2
| 按位或
6 | 5 = 7
110
| 101
-----
111 -->7
^ 按位异或
相同为false 0 不同为true 1
一个数异或一个数两次,结果不变
6 ^ 5 = 3
110
^ 101
-----
011 -->3
~ 按位取反
0-->1 1-->0
小知识:两个整数交换值的三种方法
1.使用第三方变量
2.int a,int b;
a=a+b;
b=a-b;
a=a-b;
3.int n=3,m=6;
n = n^m;
m = n^m;
n = n^m;
字符串链接符号: +
字符串数据和任何数据使用 + 都是你相连接的意思 最后都会变成字符串
System.out.println("6+6"+6+6);
三元运算符/三目运算符
格式: 关系表达式 ? 表达式1 : 表达式2
boolean
关系表达式结果为true 则判断表达式1 false则判断表达式2
class Demo_4
{
public static void main(String[] args)
{
//1.用三目运算符 比较两个整数是否相等
int a = 10;
int b = 20;
boolean b1 = (a==b) ? true : false;
System.out.println(b1);
//2.获取三个整数中的最大值
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int temp = (a>b)?a:b;
int max = (temp>c)?temp:c;
System.out.println(max);
}
}
10.运算符的优先级和转义字符
赋值运算符 从左往右看
转义字符:用在字符串中 用来定制字符串的输出格式的特殊符号
t 一个退格键
n 换行 (linux unix)
r 回车(在windows下 换行 rn)
' 单引号
" 双引号
\
String s="哥们儿,rn最近还好吗?t有没有看rn"星球大战"?'男'猪脚\叫什么来着?";
程序流程控制,结构化程序有三种结构
1.顺序结构:从上向下 一行行代码执行
2.分支结构(选择结构)
3.循环结构
二,分支语句
为什么要用分支?
当程序面临选择的时候,每一种选择对应一种操作(结果)
1.if语句
1.1
结构:if(表达式) --->关系表达式 和 逻辑表达式组成
{
执行语句;
}
执行流程:
a.当程序执行到if的时候 先去判断表达式分的结果 true还是false
b.如果表达式的值是true 就执行{}里的语句 一条if语句结束
c.如果表达式的值是false 就不执行{}里的语句
2.if...else语句
2.1
结构:if(表达式) --->关系表达式 和 逻辑表达式组成
{
执行语句;
}
else
{
执行语句;
}
执行流程:
a.当程序执行到if的时候 先去判断表达式分的结果 true还是false
b.如果表达式的值是true 就执行{}里的语句 一条if语句结束
c.如果表达式的值是false 就执行else{}里的语句
import java.util.Scanner;
class Demo_6
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
/*int a=sc.nextInt();
int b= sc.nextInt();
if(a>b)
{
System.out.println(a+" "+b);
}
if(b>a)
{
System.out.println(b+""+a);
}*/
/*int c= sc.nextInt();
if(c%2==0)
{
System.out.println(c+"是偶数");
}
else
{
System.out.println(c+"是奇数");
}*/
String s= sc.next();
char a=s.charAt(0);
if(a=>65&&a<=90)
{
a +=32;
}
System.out.println(a);
//System.out.println("哥们儿,rn最近还好吗?t有没有看rn"星球大战"?'男'猪脚\叫什么来着?");
}
}
家庭作业!!!
1.输入1~7显示对应的日期
2.判断质数
import java.util.Scanner;
import java.lang.Math;
class Homework
{
public static void main(String[] args)
{
homework2();
}
public static void homework1()
{
Scanner sc = new Scanner(System.in);
int a= sc.nextInt();
if(a==1)
{
System.out.println("今天是星期一");
}
if(a==2)
{
System.out.println("今天是星期二");
}
if(a==3)
{
System.out.println("今天是星期三");
}
if(a==4)
{
System.out.println("今天是星期四");
}
if(a==5)
{
System.out.println("今天是星期五");
}
if(a==6)
{
System.out.println("今天是星期六");
}
if(a==7)
{
System.out.println("今天是星期天");
}
}
public static void homework2()
{
Scanner sc= new Scanner(System.in);
int a=sc.nextInt();
int k=(int)Math.sqrt(a);
int i;
for(i=2;i<=k;i++)
{
if(a%i==0)
break;
}
if(i>k)
System.out.println("这个数是质数");
else
System.out.println("这个数不是质数");
}
}
今天依旧手残!70字/秒!
最后
以上就是搞怪过客为你收集整理的Java基础篇运算符与if判断的全部内容,希望文章能够帮你解决Java基础篇运算符与if判断所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复