/*
switch(变量){
case 值1:
case 值2:
case 值3:
default:
}
1.根据变量的值,选择相应的case去判断,一旦满足case条件,就执行case的相应语句。如果没有break或者已经
到结尾的话,会继续执行其下的case语句。
2.default:是可选的,而且位置是灵活的。
3.变量可以是哪些类型?char byte short int 枚举 String(jdk1.7)
4.case 条件:其中条件只能是值,不能是取值范围!
*/
class TestSwitch1 {
public static void main(String[] args) {
int i = 2;
switch(i){
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
default:
System.out.println("other");
break;
}
/*
switch(i){
case (i > 2):
System.out.println("a");
break;
case (i > 5):
System.out.println("b");
break;
default:
System.out.println("c");
break;
}
*/
}
}
switch语句应用举例
class TestSwitch2{
public static void main(String[] args) {
String season = "SPRING";
switch(season){
case "SPRING":
System.out.println("春天");
break;
case "SUMMER":
System.out.println("夏天");
break;
case "AUTUMN":
System.out.println("秋天");
break;
case "WINTER":
System.out.println("冬天");
break;
default:
System.out.println("输入有误");
break;
}
}
}
案例三:
对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”
/*
对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。
说明:
1.当多个case语句处理的语句块一致时,可以统一来书写
2.一定情况下switch-case与if-else之间可以相互转换。
*/
class TestSwitch3 {
public static void main(String[] args){
int score = 67;
if(score >= 60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
/*
switch(score){
case 100:
...
case 0:
default:
}
*/
switch(score / 10){
case 10:
case 9:
case 8:
case 7:
case 6:
System.out.println("及格");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
System.out.println("不及格");
break;
default:
System.out.println("输入有误");
break;
}
}
}
练习:
根据用于指定月份,打印该月份所属的季节。
3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
编写程序:从键盘上输入2016年的“month”和“day”,要求通过程序输出输入的日期为2014年的第几天。
/*
编写程序:从键盘上输入2014年的“month”和“day”,要求通过程序输出输入的日期为2014年的第几天
*/
import java.util.Scanner;
class TestSwitch4{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入月份:");
int month = s.nextInt();
System.out.println("请输入日期:");
int day = s.nextInt();
int sum = 0;//用来记录month月day日是2014年第几天
//使用switch-case
switch(month){
case 12:
sum += 30;//30:十一月份的总天数
case 11:
sum += 31;
case 10:
sum += 30;
case 9:
sum += 31;
case 8:
sum += 31;
case 7:
sum += 30;
case 6:
sum += 31;
case 5:
sum += 30;
case 4:
sum += 31;
case 3:
sum += 28;
case 2:
sum += 31;//31:是一月份的天数
case 1:
sum += day;
}
System.out.println(sum);
}
}
循环结构
循环语句功能
在某些条件满足的情况下,反复执行特定代码的功能
循环语句的四个组成部分
初始化部分(init_statement)
循环条件部分(test_exp)
循环体部分(body_statement)
迭代部分(alter_statement)
循环语句分类
for 循环
while 循环
do/while 循环
for 循环
案例:
/*
for循环结构:
1.格式:
①初始化条件
②循环条件
③迭代条件
④循环体
for(①;②;③){
//④
}
2.执行过程:①-②-④-③-②-④-③-。。。-④-③-②
即直至循环条件不满足,退出当前的循环
*/
class TestFor{
public static void main(String[] args) {
System.out.println("Hello World!" );
System.out.println("Hello World!" );
System.out.println("Hello World!" );
System.out.println("Hello World!" );
System.out.println();
for(int i = 0;i < 4;i++){
System.out.println("Hello World!" );
}
//System.out.println(i);
int j = 1;
for(System.out.print('a');j < 3;System.out.print('b'),j++){
System.out.print('c');
}
//acbcb
System.out.println();
//题目:输出100以内的所有偶数及所有偶数的和(累加的思想)及偶数的个数
int sum = 0;//用来记录所有偶数的和
int count = 0;
for(int i = 1;i <= 100;i++){//100以内的自然数的遍历
if(i % 2 == 0){
System.out.println(i);
sum += i;
count++;
}
}
System.out.println("总和为:" + sum);
System.out.println("100以内偶数的个数为:" + count);
}
}
小测试:
编写程序FooBizBaz.java,从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出“foo”,在每个5的倍数行上打印“biz”,在每个7的倍数行上打印输出“baz”。
对应的代码:
/*
编写程序FooBizBaz.java,从1循环到150并在每行打印一个值,
另外在每个3的倍数行上打印出“foo”,在每个5的倍数行上打印“biz”,
在每个7的倍数行上打印输出“baz”。
*/
class FooBizBaz
{
public static void main(String[] args)
{
for(int i = 1;i <= 150;i++){
System.out.print(i + " ");
if(i % 3 == 0){
System.out.print("foo ");
}
if(i % 5 == 0){
System.out.print("biz ");
}
- if(i % 7 == 0){
System.out.print("baz");
}
System.out.println();
}
}
}
练习:输出所有的水仙花数,所谓水仙花数是指一个3
位数,其各个位上数字立方和等于其本身。
例如: 153 = 1*1*1 + 3*3*3 + 5*5*5
/*
输出所有的水仙花数,所谓水仙花数是指一个3
位数,其各个位上数字立方和等于其本身。
例如: 153 = 1*1*1 + 3*3*3 + 5*5*5
*/
class ShuiXianHua{
public static void main(String[] args) {
for(int i = 100;i < 1000;i++){//实现100-999的遍历 456 - 400 = 56 - 50
int j1 = i / 100;//百位
int j2 = (i - j1*100)/10;//十位
int j3 = i % 10;//个位
if(i == j1*j1*j1 + j2*j2*j2 + j3*j3*j3){
System.out.println(i);
}
}
}
}
While循环
语法格式
[初始化语句]
while( 布尔值测试表达式){
语句或语句块;
[更改语句;]
}
应用举例
public class WhileLoop {
public static void main(String args[]){
int result = 0;
int i=1;
while(i<=100) {
result += i;
i++;
}
System.out.println("result=" + result);
}
While案例:
/*
①初始化条件
②循环条件
③迭代条件
④循环体
1.格式:
①
while(②){
④
③
}
2.for循环与while可以相互转换。
*/
class TestWhile{
public static void main(String[] args) {
//100以内的偶数的输出,及其和
int i = 1;
int sum = 0;
while(i <= 100){
if(i % 2 == 0){
System.out.println(i);
sum += i;
}
i++;
}
System.out.println(sum);
//System.out.println(i);
}
}
do-while 循环语句
语法格式
[初始化语句]
do{
语句或语句块;
[更改语句;]
}while(布尔值测试表达式);
/*
①初始化条件
②循环条件
③迭代条件
④循环体
1.格式:
①
do{
④
③
}while(②);
2.do-while与while的区别:do-while循环至少会执行一次!
*/
class TestDoWhile{
public static void main(String[] args) {
int i = 1;
do{
if(i % 2 == 0){
System.out.println(i);
}
i++;
}while(i <= 100);
//do-while与while的区别
int j = 10;
do{
System.out.println(j);
j++;
}while(j < 10);
while(j < 10){
System.out.println(j);
j++;
}
}
}
案例:从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序。
补充:
最简单无限循环格式:while(true) , for(;;),无限循环存在的原因是并不知道循环多少次,需要根据某些条件,来控制循环。
/*
问题一:
从键盘读入个数为10个的整数,并判断读入的正数和负数的个数。
问题二:
从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序。
无限循环:
for(;;){}
或者
while(true){
}
说明:一般情况下,在无限循环内部要有程序终止的语句,使用break实现。若没有,那就是死循环!
*/
import java.util.Scanner;
class TestExer{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int a = 0;//记录正数的个数
int b = 0;//记录负数的个数
//for(;;){
while(true){
System.out.println("请输入一个整数:");
int num = s.nextInt();
if(num > 0)
a++;
else if(num < 0)
b++;
else
break;
}
/*
问题一:
for(int i = 0;i < 10;i++){
System.out.println("请输入第" + (i + 1) + "个整数");
int num = s.nextInt();
if(num > 0)
a++;
else if(num < 0)
b++;
}
*/
System.out.println("正数的个数为:" + a);
System.out.println("负数的个数为:" + b);
}
}
嵌套循环
将一个循环放在另一个循环体内,就形成了嵌套循环。其中,for ,while ,do…while均可以作为外层循环和内层循环。
案例:
/*
嵌套循环:循环中还可以声明循环。相当于内层循环的整体充当外层循环的循环体
for(;;){
for(;;){
}
}
或者
while(){
for(;;){}
}
*/
class TestForFor{
public static void main(String[] args) {
/*
*****
*****
*****
*****
*/
for(int j = 0;j < 4;j++){//外层循环控制行数
for(int i = 0;i < 5;i++){//内层循环控制列数
System.out.print("*");
}
System.out.println();
}
/*
*
**
***
****
*/
for(int j = 0;j < 4;j++){//外层循环控制行数
for(int i = 0;i < j + 1;i++){//每行输出j + 1个
System.out.print("*");
}
System.out.println();
}
/*
*****
****
***
**
*
*/
for(int i = 0; i < 5;i++){
for(int j = 0;j < 5-i;j++){//for(int j = i;j < 5;j++)
System.out.print("*");
}
System.out.println();
}
/*
*
**
***
****
*****
****
***
**
*
*/
for(int j = 0;j < 4;j++){//外层循环控制行数
for(int i = 0;i < j + 1;i++){//每行输出j + 1个
System.out.print("*");
}
System.out.println();
}
for(int i = 0; i < 5;i++){
for(int j = 0;j < 5-i;j++){//for(int j = i;j < 5;j++)
System.out.print("*");
}
System.out.println();
}
/*
打印如下图形
----*
---* *
--* * *
-* * * *
* * * * *
ik-j* k = 4 - i j = i + 1
041
132
223
314
405
-* * * *
--* * *
---* *
----*
ik-j* k = i + 1 j = 4 - i
014
123
232
341
*/
//上半部分:
for(int i = 0;i < 5;i++){
for(int k = 0;k < 4 - i;k++){
System.out.print(" ");
}
for(int j = 0;j < i + 1;j++){
System.out.print("* ");
}
System.out.println();
}
//下半部分
for(int i = 0;i < 4;i++){
for(int k = 0;k < i + 1;k++){
System.out.print(" ");
}
for(int j = 0;j < 4 - i;j++){
System.out.print("* ");
}
System.out.println();
}
}
}
/*
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
...
9 * 1 = 9 .... 9 * 9 = 81
*/
class TestJiuJiu{
public static void main(String[] args){
for(int i = 1;i <= 9;i++){//一共有9行
for(int j = 1;j <= i;j++){//每行有i个等式
System.out.print(i + "*" + j + "=" + i*j + "\t");
}
System.out.println();
}
}
}
特殊流程控制语句1
break 语句
break语句用于终止某个语句块的执行
{ ……
break;
……
}
break 语句用法举例
public class TestBreak{
public static void main(String args[]){
for(int i = 0; i<10; i++){
if(i==3)
break;
System.out.println(" i =" + i);
}
System.out.println("Game Over!");
}
}
continue 语句
continue语句用于跳过某个循环语句块的一次执行
continue语句出现在多层嵌套的循环语句体中时,可以通过标签指明要跳过的是哪一层循环
continue语句用法举例
public class ContinueTest {
public static void main(String args[]){
for (int i = 0; i < 100; i++) {
if (i%10==0)
continue;
System.out.println(i);
} } }
/*
break:使用在switch-case中或者循环中
如果使用在循环中,表示:结束"当前"循环
continue:使用在循环结构中
表示:结束"当次"循环
关于break和continue中标签的使用。(理解)
*/
class TestBreakContinue{
public static void main(String[] args) {
/*
for(int i = 1;i <= 10;i++){
if(i % 4 == 0){
//break;
continue;
}
System.out.println(i);
}
*/
for(int i = 1;i < 5;i++){
for(int j = 1;j <= 10;j++){
if(j % 4 == 0){
//break;
//continue;
//break label;
continue;
}
System.out.print(j);
}
System.out.println();
}
System.out.println();
for(int i = 1;i <= 10;i++){
if(i % 4 == 0){
break;
//continue;
//在break和continue语句之后不能添加其他语句,应为永远也不可能被执行!
System.out.println("林志玲晚上要约我");
}
System.out.println(i);
}
}
}
break只能用于switch语句和循环语句中。
continue 只能用于循环语句中。
二者功能类似,但continue是终止本次循环,break是终止本层循环。
break、continue之后不能有其他的语句,因为程序永远不会执行其后的语句。
标号语句必须紧接在循环的头部。标号语句不能用在非循环语句的前面。