1.1. 基本语句
1.1.1. if语句
1.格式一:
if ( 条件表达式 ) {
执行语句;
}
例子:
注意:
如果if语句中只有一条语句,那么可以不写大括号。
2.格式二:
if ( 条件表达式 ) {
执行语句;
} else {
执行语句;
}
例子1:
例子2:
注意:
三元运算符就是if else语句的简写格式。
例如:b = a > 1 ? 100 : 200;就可以实现和上面同样的功能。
简写格式什么时候用?
当if else运算后,有一个具体的结果时,可以简化写成三元运算符。
3.格式三:
if(条件表达式) {
执行语句;
} else if (条件表达式) {
执行语句;
}
……
else {
执行语句;
}
例子:
if语句特点:
1、每一种格式都是单条语句。
2、第二种格式与三元运算符的区别:三元运算符运算完要有值出现。好处是:可以写在其他表达式中。
3、条件表达式无论写成什么样子,只看最终的结构是否是true或者false。
练习1:
IfDemo.java
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");
结果:
练习2:
IfDemo.java
1.1.2. 选择结构,switch语句
格式:
switch(表达式)
{
case 取值1:
执行语句;
break;
case 取值2:
执行语句;
break;
……
default:
执行语句;
break;
}
switch语句特点:
1、switch语句选择的类型只有四种:byte,short,int,char。
2、case与default没有顺序。先执行第一个case,没有匹配的case执行default。
3、结束switch语句的两种情况:①遇到break,②执行到switch语句结束。
4、如果匹配的case或者default没有对应的break,那么程序会继续向下执行,运行可以执行的语句,直到遇到break或者switch结尾结束。
5、进入switch语句后,执行顺序是先执行case,然后从上到下,最后再执行default。即使default放在case上面,执行顺序也不变。
例子1:
SwitchDemo.java
class SwitchDemo {
public static void main(String[] args) {
int x = 3;
switch (x) {
default:
System.out.println("d");
break;
case 4:
System.out.println("a");
break;
case 2:
System.out.println("b");
break;
case 3:
System.out.println("c");
break;
}
}
}
结果:
c
例子2:
SwitchDemo.java
class SwitchDemo {
public static void main(String[] args) {
int a = 4, b = 2;
char opr = '-';
switch (opr) {
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("无法运算,符号不支持");
break;
}
}
}
结果:
例子3:部分case和default中没有break语句的情况。
SwitchDemo.java
class SwitchDemo {
public static void main(String[] args) {
int x = 3;
switch (x) {
case 4:
System.out.println("a");
break;
case 2:
System.out.println("b");
case 3:
System.out.println("c");
default:
System.out.println("d");
}
}
}
结果:
例子4:
SwitchDemo.java
class SwitchDemo {
public static void main(String[] args) {
/*
* 用户输入的数据对应的星期。
*/
int week = 4;
switch (week) {
case 1:
System.out.println(week + "对应的是星期一");
break;
case 2:
System.out.println(week + "对应的是星期二");
break;
case 3:
System.out.println(week + "对应的是星期三");
break;
case 4:
System.out.println(week + "对应的是星期四");
break;
case 5:
System.out.println(week + "对应的是星期五");
break;
case 6:
System.out.println(week + "对应的是星期六");
break;
case 7:
System.out.println(week + "对应的是星期日");
break;
default:
System.out.println(week + "没有对应的星期");
break;
}
}
}
结果:
例子5:多个case同一种处理方式的情况。
SwitchDemo.java
class SwitchDemo {
public static void main(String[] args) {
int month = 3;
switch (month) {
case 3:
case 4:
case 5:
System.out.println(month + "月对应的是春季");
case 6:
case 7:
case 8:
System.out.println(month + "月对应的是夏季");
case 9:
case 10:
case 11:
System.out.println(month + "月对应的是秋季");
case 12:
case 1:
case 2:
System.out.println(month + "月对应的是冬季");
default:
System.out.println(month + "没有对应的季节");
}
}
}
结果:
1.1.3. if和switch的应用:
if:
1、对具体的值进行判断。
2、对区间判断。
3、对运算结果是boolean类型的表达式进行判断。
switch:
1、对具体的值进行判断。
2、值的个数通常是固定的。
对于几个固定的值判断,建议使用switch语句,因为switch语句会将具体的答案都加载进内存,效率相对高。
1.1.4. 循环结构
1.1.4.1. while循环
关键词:while,do while,for。
while语句格式:
while( 条件表达式 ) {
执行语句;
}
例子:
WhileDemo.java
class WhileDemo {
public static void main(String[] args) {
int x = 1;
while (x < 3) {
System.out.println("x = " + x);
x++;
}
}
}
结果:
注意:
一定要注意不要写while(x < 3);这样的语句,后面的分号就是循环体,代表不执行任何语句,这个循环就成了一个死循环。
do while语句格式:
do {
执行语句;
} while( 条件表达式 );
例子:
DoWhileDemo.java
class DoWhileDemo {
public static void main(String[] args) {
int x = 1;
do {
System.out.println("x = " + x);
x++;
} while (x < 3);
}
}
结果:
while和do while的区别
do while语句的特点:无论条件是否满足,循环体至少执行一次。
while如果条件不满足,循环体一次都不会执行。
练习1:获取1到10,10个数字的和。
累加思想
WhileDemo.java
class WhileDemo {
public static void main(String[] args) {
int x = 1;// 记录参与加法的数据。
int sum = 0;// 记录住每一次的和
while (x <= 10) {
sum += x;
x++;
}
System.out.println("sum = " + sum);
}
}
结果:
练习2:1~100之间,6的倍数出现的次数。
计数器思想
WhileDemo.java
class WhileDemo {
public static void main(String[] args) {
int x = 1;
int count = 0;
while (x <= 100) {
if (x % 6 == 0) {
count++;
}
x++;
}
System.out.println("count = " + count);
}
}
结果:
1.1.4.2. for循环
For语句格式:
for(初始化表达式;循环条件表达式;循环后的操作表达式)
{
执行语句;(循环体)
}
for里面的三个表达式运行的顺序,初始化表达式只读一次,判断循环条件,为真就执行循环体,然后再执行循环后的操作表达式,接着继续判断循环条件,重复找个过程,直到条件不满足为止。
ForDemo.java
class ForDemo {
public static void main(String[] args) {
for (int x = 0; x < 3; x++) {
System.out.println("x = " + x);
}
}
}
结果:
注意:for循环的初始化表达式、循环后的操作表达式可以是多个表达式,通过逗号分隔。
例如:
for(int a = 1,b =2; a < 2 & b < 3; a++,b++){
}
练习1:
ForDemo.java
class forDemo {
public static void main(String[] args) {
int x = 1;
for (System.out.println("a"); x < 3; System.out.println("c")) {
System.out.println("d");
x++;
}
}
}
结果:
1.1.4.3. 循环注意点
1、while与for可以互换,区别在于for为了循环而定义的变量在for循环结束就在内存中释放。而while循环使用的变量在循环结束后还可以继续使用。
下面通过两个例子对比:
WhileDemo.java
class whileDemo {
public static void main(String[] args) {
// 打印1~10十个数字
int x = 1;
while (x < 10) {
System.out.println("x = " + x);
x++;
}
System.out.println("x = " + x);
}
}
结果:
ForDemo.java
class ForDemo {
public static void main(String[] args) {
for (int y = 1; y < 10; y++) {
System.out.println("y = " + y);
}
System.out.println("y = " + y);
}
}
结果:
2、最简单无限循环格式:while(true) , for(;;),无限循环存在的原因是并不知道循环多少次,而是根据某些条件,来控制循环。
3、在使用循环时候,一定要明确哪些语句需要参与循环,哪些不需要。循环通常情况下,需要定义条件,需要控制次数。
1.1.4.4. 循环嵌套
For循环嵌套例子:
例子1:
ForForDemo.java
class ForForDemo {
public static void main(String[] args) {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 4; y++) {
System.out.println("ok");
}
}
}
}
结果:
例子2:
ForForDemo.java
class ForForDemo {
public static void main(String[] args) {
for (int x = 0; x < 4; x++) { // 外循环控制的是行数
for (int y = 0; y < 5; y++) {
System.out.print("*");
}
System.out.println();
}
}
}
结果:
练习1:
打印出下格式的内容:
*****
****
***
**
*
答案1:
ForForDemo.java
class ForForDemo {
public static void main(String[] args) {
int z = 5;
for (int x = 0; x <= 4; x++) {
for (int y = 1; y <= 5 - x; y++) {
System.out.print("*");
}
System.out.println();
z--;
}
}
}
结果:
答案2:
class ForForDemo {
public static void main(String[] args) {
for (int x = 1; x <= 5; x++) {
for (int y = x; y <= 5; y++) {
System.out.print("*");
}
System.out.println();
}
}
}
结果:
练习2:
打印出下格式的内容:
*
**
***
****
*****
答案:
ForForDemo.java
class ForForDemo {
public static void main(String[] args) {
for (int x = 1; x <= 5; x++) {
for (int y = 1; y <= x; y++) {
System.out.print("*");
}
System.out.println();
}
}
}
结果:
练习3:
打印出下格式的内容:
54321
5432
543
54
5
答案:
ForForDemo.java
class ForForDemo {
public static void main(String[] args) {
for (int x = 1; x <= 5; x++) {
for (int y = 5; y >= x; y--) {
System.out.print(y);
}
System.out.println();
}
}
}
结果:
练习4:
打印出下格式的内容:
1
22
333
4444
55555
答案:
ForForDemo.java
class ForForDemo {
public static void main(String[] args) {
for (int x = 1; x <= 5; x++) {
for (int y = 1; y <= x; y++) {
System.out.print(x);
}
System.out.println();
}
}
}
结果:
练习5:
打印九九乘法表成如下形式:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
...
答案:
ForForDemo.java
class ForForDemo {
public static void main(String[] args) {
for (int x = 1; x <= 9; x++) {
for (int y = 1; y <= x; y++) {
System.out.print(y + "*" + x + "=" + (x * y) + "\t");
}
System.out.println();
}
}
}
结果:
注意:
代码中的"\t"是一个转义字符,也就是制表符。还有其他的一些转义字符:
\n:回车,\b:退格,\r:按下回车符。
windows系统中回车符其实是由两个转义字符组成的:\r\n。
linux中回车符是\n。
例子:
//打印"hello world":
System.out.println("\"hello word\"");
//打印\hello world\:
System.out.println("\\hello word\\");
练习6:
打印出下格式的内容:
* * * * *
* * * *
* * *
* *
*
答案:
ForForDemo.java
class ForForDemo {
public static void main(String[] args) {
for (int x = 1; x <= 5; x++) {
// 首先打印出*前面的空格
for (int y = 1; y < x; y++) {
System.out.print(" ");
}
// 再打印出*
for (int z = x; z <= 5; z++) {
System.out.print("* ");
}
System.out.println();
}
}
}
结果:
1.1.4.5. break(跳出)与continue(继续)
break语句:
应用范围:选择结构和循环结构。
例子1:
BreakDemo.java
class BreakDemo {
public static void main(String[] args) {
for (int x = 0; x < 3; x++) {
System.out.println("x = " + x);
break;
}
}
}
结果:
例子2:
BreakDemo.java
class BreakDemo {
public static void main(String[] args) {
for (int x = 0; x < 3; x++) {
if (x == 1)
break;
System.out.println("x = " + x);
}
}
}
结果:
例子3:
class BreakDemo {
public static void main(String[] args) {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 4; y++) {
System.out.println("x = " + x);
break;
}
}
}
}
结果:
continue语句:
应用范围:循环结构。
continue语句是结束本次循环继续下次循环。
例子:
ContinueDemo.java
class ContinueDemo {
public static void main(String[] args) {
for (int x = 0; x < 11; x++) {
if (x % 2 == 0)
continue;
System.out.println("x = " + x);
}
}
}
结果:
注意:
1、这两个语句离开应用范围,存在是没有意义的。
2、这个两个语句单独存在下面都不可以有语句,因为执行不到。
例子1:
BreakDemo.java
class BreakDemo {
public static void main(String[] args) {
for (int x = 0; x < 3; x++) {
break;
System.out.println("x = " + x);
}
}
}
结果:
例子2:
ContinueDemo.java
class ContinueDemo {
public static void main(String[] args) {
for (int x = 0; x < 3; x++) {
continue;
System.out.println("x = " + x);
}
}
}
结果:
3、标号的出现,可以让这两个语句作用于指定的范围。
例子1:
BreakDemo.java
class BreakDemo {
public static void main(String[] args) {
out: for (int x = 0; x < 3; x++) {
in: for (int y = 0; y < 4; y++) {
System.out.println("x = " + x);
break out;
}
}
}
}
结果:
例子2:
ContinueDemo.java
class ContinueDemo {
public static void main(String[] args) {
out: for (int x = 0; x < 3; x++) {
in: for (int y = 0; y < 4; y++) {
System.out.println("x = " + x);
continue out;
}
}
}
}
结果: