---------------------if语句介绍---------------------------------------------------

class IfDemo
{
public static void main(String[] args)
{
int x = 1;

if(x>1)
{
System.out.println("yes");
}
else
{
System.out.println("a");
}

/*
if else 结构 简写格式: 变量 = (条件表达式)?表达式1:表达式2;

三元运算符:
好处:可以简化if else代码。
弊端:因为是一个运算符,所以运算完必须要有一个结果。
*/
int a = 9,b;
b = (a>1)?100:200;

if(a>1)
b = 100;
else
b = 200;

int n = 3;

if(n>1)
System.out.println("a");
else if(n>2)
System.out.println("b");
else if(n>3)
System.out.println("c");
else
System.out.println("d");

/*
if(n>1)
System.out.println("a");
if(n>2)
System.out.println("b");
if(n>3)
System.out.println("c");
else
System.out.println("d");
*/
System.out.println("over");
}
}

--------------------------------if语句练习-----------------------------------------------------

class IfTest
{
public static void main(String[] args)
{
//需求1:根据用户定义的数值不同。打印对应的星期英文。
/*
int num = 1;

if(num==1)
System.out.println("monday");
else if(num==2)
System.out.println("tsd");
else
System.out.println("nono");
*/
//需求2:根据用于指定月份,打印该月份所属的季节。
//3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季

int x = 4;

if(x==3 || x==4 || x==5)
System.out.println(x+"春季");
else if(x==6 || x==7 || x==8)
System.out.println(x+"夏季");
else if(x==9 || x==10 || x==11)
System.out.println(x+"秋季");
else if(x==12 || x==1 || x==2)
System.out.println(x+"冬季");
else
System.out.println(x+"月份不存在");

if(x>12 || x<1)
System.out.println(x+"月份不存在");
else if(x>=3 && x<=5)
System.out.println(x+"春季");
else if(x>=6 && x<=8)
System.out.println(x+"夏季");
else if(x>=9 && x<=11)
System.out.println(x+"秋季");
else
System.out.println(x+"冬季");
}
}

-------------------------------常见操作符--------------------------------------

class OperateDemo
{
public static void main(String[] args)
{
//int x = 4270;

//x = x /1000 * 1000;
//System.out.println(-1%5);

int a = 3,b;

//a++; //--> a = a+ 1;

b = ++a;

System.out.println("a="+a);
//字符串数据和任何数据使用+都是相连接,最终都会变成字符串。
//System.out.println("5+5"+(5+5));//"5+5=55"

/*
转义字符:通过\ 来转变后面字母或者符号的含义。
\n:换行。
\b:退格。相当于backspace。
\r:按下回车键。window系统,回车符是由两个字符来表示\r\n.
\t:制表符。相当于tab键。
*/
System.out.println("hello \t world");
//System.out.println("hello java");

System.out.println("\\hello\\");

char ch = '\'';

char c = 'a';

}
}

-----------------------------------------------------------------------------------

class OperateDemo1
{
public static void main(String[] args)
{
int x = 3;

//+= -= *= /= %=

x+=4;//x = x + 4;

short s = 4;

//s = s + 5;
s+=5;

int a,b,c;
a = b = c = 5;

System.out.println(3=4);
}
}

------------------------------一些简单运算符的基本运算-----------------------------------------

class OperateDemo2
{
public static void main(String[] args)
{

int x = 7;

//逻辑运算符用于连接boolean类型的表达式。

//x>3 & x<6 = true & true = true;
/*
true & true = true;
true & false = false;
false & true = false;
false & false = false;

& : 只要两边的boolean表达式结果,有一个为false。那么结果就是false。
只有两边都为true,结果为true。
*/
/*
true | true = true;
true | false = true;
false | true = true;
false | false = false;
| : 两边只要有一个为true,结果为true。
只有两边都有false,结果为false。
*/

/*
^ : 异或;就是和|有点不一样。当true ^ true = false;
true ^ true = false;
true ^ false = true;
false ^ true = true;
false ^ false = false;
^ : 两边相同结果是false。
两边不同结果是true。

*/

/*
!!true
*/

int a = 2;

//a>3 && a<6;

/*
&和&&的特点:
&:无论左边是true是false。右边都运算。
&&:当左边为false时,右边不运算。

|:两边都参与运算。
||:当左边为true。右边不运算。
*/

System.out.println(false ^ false);
System.out.println(~6);

int n = 3,m = 8;
System.out.println("n="+n+",m="+m);

}

----------------------------------------------------------------------

class OperateDemo3
{
public static void main(String[] args)
{

//System.out.println(Integer.toBinaryString(60));
//System.out.println(Integer.toHexString(60));

int num = 26;

//获取60的最低4位,通过&15;
int n1 = num & 15;

System.out.println(n1>9?(char)(n1-10+'A'):n1);

//要获取下一组四位,将60右移4位。
int temp = num >>> 4;

//对temp的值进行最低四位的获取。
int n2 = temp & 15;
System.out.println(n2>9?(char)(n2-10+'A'):n2);

/*
0-9 'A' 'B' 'C' 'D' 'E' 'F'
65 66 67
10 11 12 13 14 15

12 - 10 = 2 + 'A' = (char)67;
*/

int x = 1,y;

y = (x>1)?'a':200;
System.out.println("y="+y);
}
}

----------------------------------switch语句介绍------------------------------------------------

class SwitchDemo
{
public static void main(String[] args)
{

int x = 3;
/*
switch(x)//byte short int char
{
default:
System.out.println("d");
//break;
case 4:
System.out.println("a");
//break;
case 6:
System.out.println("b");
break;
case 2:
System.out.println("c");
break;

}
*/

int a=4,b =2;

char ch = '+';

switch(ch)
{
case '-':
System.out.println(a-b);
break;
case '+':
System.out.println(a+b);
break;
case '*':
System.out.println(a*b);
break;
case '/':
System.out.println(a/b);
break;
default:
System.out.println("feifa");

}

System.out.println("Hello World!");
}
}

----------------------------switch语句练习-----------------------------------------

class SwitchTest
{
public static void main(String[] args)
{

//需求2:根据用于指定月份,打印该月份所属的季节。
//3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季

int x = 4;

switch(x)
{
case 3:
case 4:
case 5:
System.out.println(x+"春季");
break;

case 6:
case 7:
case 8:
System.out.println(x+"夏季");
break;
case 9:
case 10:
case 11:
System.out.println(x+"秋季");
break;
case 12:
case 1:
case 2:
System.out.println(x+"冬季");
break;
default:
System.out.println("nono");

}

/*
if和switch语句很像。
具体什么场景下,应用哪个语句呢?
如果判断的具体数值不多,而是符合byte short int char这四种类型。
虽然两个语句都可以使用,建议使用swtich语句。因为效率稍高。

其他情况:对区间判断,对结果为boolean类型判断,使用if,if的使用范围更广。

*/
System.out.println("Hello World!");
}
}

----------------------------------------------------------------------------------------------------------

class VarDemo
{
public static void main(String[] args)
{
/*
//定义变量的格式;
//数据类型 变量名 = 初始化值;
// 定义一个int类型变量.取值为4;
int x = 4;
System.out.println(x);
x = 10;
System.out.println(x);

//演示其他数据类型。

byte b = 2;//-128~127;

//byte b1 = 128;

short s = 30000;

long l = 4l;

float f = 2.3f;

double d = 34.56;

char ch = '4';
char ch1 = 'a';
char ch2 = '+';
char ch3 = ' ';

boolean bo = true;
boolean bo1 = false;

int a = 5;
a = a + 6;
*/
/*
什么时候定义变量?
当数据不确定的时候。需要对数据进行存储时。
就定义一个变量来完成存储动作。

*/

//类型的转换。

//byte b = 3;

//b = b + 2;

//System.out.println(b);

//System.out.println((char)5);

byte b = 3;

b = 3 + 4;
//b = b + 4;

System.out.println(b);

}
}

最新文章

  1. 使用struct处理二进制
  2. C#、JAVA操作Hadoop(HDFS、Map/Reduce)真实过程概述。组件、源码下载。无法解决:Response status code does not indicate success: 500。
  3. icomoon图标的使用
  4. android小技巧(一)
  5. error LNK2005: _DllMain@12 已经在 dllmain.obj 中定义
  6. paip.php 5.0 5.3 5.4 5.5 -6.0的新特性总结与比较
  7. 解决Eclipse快捷键被其他软件占用
  8. jstl表达式使用方法
  9. H5页面请求跨域问题
  10. Bootstrap_表单_表单提示信息
  11. Python中lambda用法
  12. mac配置java和maven环境变量
  13. IntelliJ IDEA 教程(1)
  14. C++读写图片数据转成Base64格式
  15. Java核心技术卷一基础知识-第2章-Java程序设计环境-读书笔记
  16. gometalinter代码质量检查分析工具(golang)
  17. 清除redis缓存
  18. [maven] dependency标签理解
  19. 分析java内存情况
  20. [pytorch修改]npyio.py 实现在标签中使用两种delimiter分割文件的行

热门文章

  1. time &amp; datetime
  2. awk中的system和getline的用法
  3. AOJ DSL_2_A Range Minimum Query (RMQ)
  4. RabbitMQ 参数们的Power &ldquo;续&rdquo;
  5. JS 验证数组中是否包含重复元素
  6. windows下使用pip安装python的第三方lxml库
  7. Tp-link TL-WR841N无线路由器端口映射到外网如何设置
  8. ES5语法
  9. javascript设置和获取cookie的通用方法
  10. (转载)PHPCMS V9专题路径多了一个斜杠的解决办法