【Java 基础篇】【第三课】表达式、控制结构
package a.b;
public class Three
{
static void Expression()
{
System.out.println("一、学习基本的表达式");
// 数学表达式
System.out.println( 1 + 2 ); //加法
System.out.println( 4 - 3.2 ); //减法
System.out.println( 7 * 1.5); //乘法
System.out.println( 29/7 ); //除法
System.out.println( 29%7 ); //求余
// 关系表达式
System.out.println(6 > 4.2); //大于
System.out.println(3.4 >= 3.4); //大于等于
System.out.println(1.5 < 9 ); //小于
System.out.println( 6 <= 1); // 小于等于
System.out.println(2 == 2); //等于
System.out.println(2 != 2 ); //不等于
/*
其他的一些表达式
true && false and
(3 > 1) || (2 == 1) or
!true not
// 位运算
& and
| or
^ xor
~ not
5 << 3 0b101 left shift 3 bits
6 >> 1 0b110 right shift 1 bit
m++ 变量m加1
n-- 变量n减1
condition ? x1 : x2 condition为一个boolean值。根据condition,取x1或x2的值
*/
}
static void Controlstructure()
{
System.out.println("二、学习控制结构");
int a = 0;
// 选择语句
if (a == 1)
{
System.out.println("学习if语句 if ");
}
else if (a == 0)
{
System.out.println("学习if语句 else if ");
}
else
{
System.out.println("学习if语句 else ");
}
// 循环语句
while(a<3)
{
System.out.println("学习while语句"+a);
a++;
}
do
{
System.out.println("学习do while语句"+a);
a++;
}while(a<6);
// 使用break可以跳出循环、使用continue可以直接进入下一循环
for (int i = 0; i < 5; i++)
{
if (i == 1)
{
continue;
}
if (i == 4 )
{
break;
}
System.out.println("学习for语句"+i);
}
// 选择语句
char c = 'b';
switch (c)
{
case 'a':System.out.println("学习swithch语句"+c); break;
case 'b':System.out.println("学习swithch语句"+c); break;
case 'c':System.out.println("学习swithch语句"+c); break;
default:System.out.println("学习swithch语句 default"); break;
}
}
public static void main(String[] args)
{
// 学习基本的表达式
Expression();
// 学习控制结构
Controlstructure();
}
}
运行结果如下:
一、学习基本的表达式
3
0.7999999999999998
10.5
4
1
true
true
true
false
true
false
二、学习控制结构
学习
if
语句
else
if
学习
while
语句
0
学习
while
语句
1
学习
while
语句
2
学习
do
while
语句
3
学习
do
while
语句
4
学习
do
while
语句
5
学习
for
语句
0
学习
for
语句
2
学习
for
语句
3
学习swithch语句b
|
转载于:https://blog.51cto.com/xdtesting/1869725