一、if语句
/**
* if语句为判断语句,判断条件满足则执行,执行语句,
* if 如果的意思
* else 否则的意思
* else if 否则再次对判断语句进行判断
* else if 可以判断无限次
*
* int = 10;
* if(判断条件){
* 执行语句
* }else(再次判断条件){
* 执行语句
* }…无限次 else(再次判断条件)
* else{
* 最终执行语句
* }
*
* */
// if语句第一种格式
int x = 0;
if (x < 10) {
System.out.println(x);
}
// if语句的第二种格式
int y = 0;
if (y > 10) {
// y大于10吗?大于则输出。
System.out.println("y大于10");
} else {
// 否则输出下面的语句
System.out.println("y小于10");
}
// if语句的第三种格式
int z = 0;
if(z>10){
//判断z>10吗,是则输出
System.out.println("z大于10");
//否则 再进行判断,这次判断条件为z>9吗,是则输出
}else if(z>9) {
System.out.println("z大于9");
//否则再次进行判断,这次判断条件为z>8吗 是则输出
}else if(z>8){
System.out.println("z大于8");
}
// …. 无限次else if
// 否则输出下面的语句
else{
System.out.println("z原来g'h是0");
}
二、for语句
/**
* 一、for循环格式:
* for(初始化语句;判断条件语句;控制条件语句) {
* 循环体语句;
* }
*
* 二、运行:初始条件---循环体语句-----控制条件语句---判断条件语句---循环体语句...
*
* */
// for语句标准格式
for (int x = 0; x < 10; x++) {
// 输出语句,将运算结果x输出
System.out.println(x);
}
System.out.println("------------------");
// for语句的嵌套使用
for (int x = 0; x < 5; x++) {
for (int y = 0; y < 5; y++) {
System.out.println(y);
}
}
System.out.println("------------------");
// 一、(单一使用)输出1-10之间的和
int y = 0;
for (int x = 0; x < 10; x++) {
// 把y,x相加赋值给y
y += x;
// 输出每次循环后y的值
System.out.println(y);
}
// 直接输出y的最终值
System.out.println(y);
System.out.println("------------------");
//二、(单一使用) 输出1-10之间的偶数和
int z = 0;
for (int x = 1; x <= 10; x++) {
// //当x模以2等于0时,x为偶数
if (x % 2 == 0) {
// //把z,x相加赋值给z
z += x;
}
}
// 直接输出z的最终值
System.out.println(z);
System.out.println("------------------");
// 三、(单一使用)100-999 之间水仙花数
for (int x = 100; x < 999; x++) {
int ge = x % 10;
int shi = x / 10 % 10;
int bai = x / 10 / 10 % 10;
if (x == (ge * ge * ge + shi * shi * shi + bai * bai * bai)) {
System.out.println(x);
}
}
System.out.println("------------------");
//四、(嵌套使用)打印三行五列
for (int i = 0; i < 3; i++) {
for (int u = 0; u < 5; u++) {
//prinrln单词中少了;ln是有换行的意思,不写表示不换行
//每次输出一个*不换行
System.out.print("*");
}
//输出5个*后换行
System.out.println("");
}
System.out.println("------------------");
//五、(嵌套使用)打印三角形*
for (int r = 0; r < 6; r++) {
for (int t = 0; t < r; t++) {
System.out.print("*");
}
System.out.println("");
}
三、while语句
/**
*
* while循环的基本格式:
* while(判断条件语句) {
* 循环体语句;
* }
*
*
* 扩展格式:
*
* 初始化语句;
* while(判断条件语句) {
* 循环体语句;
* 控制条件语句;
* }
*
* */
int x = 0;
while( x < 10 ) {
//输出语句
System.out.println("HelloWorld");
//每次递增
x++;
}
System.out.println("-------------------------");
// 1.使用while打印1000以内的水仙花数
int z = 0;
//定义一个单独的变量
int y = 100;
while (y < 1000) {
//因为水仙花数是1000以内的所以将ge--shi--bai定义在循环内
//当y等于y模以10等于水仙花的个数时,将y赋值给ge
int ge = y % 10;
//当y等于y除以10模以10等于水仙花的个数时,将y赋值给shi
int shi = y / 10 % 10;
//当y等于y除以10除以10模以10等于水仙花的个数时,将y赋值给bai
int bai = y / 10 / 10 % 10;
//当ge--shi--bai都满足条件的情况下
if ((ge * ge * ge + shi * shi * shi + bai * bai * bai) == y) {
z++;
//输出1000内第一个到最后一个的水仙花数
System.out.println("第"+z+"个水仙花数:"+y);
}
y++;
}
//输出1000内共有几个水仙花数
System.out.println("1000以内共有" + z+"个水仙花数");
四、switch语句
/**
* switch是选择结构语句:
* 用switch(要比较的值)和case中要被比较的值进行必要
* 相等则执行里面的执行语句,否则依次比较,当所有的值
* 都不相等时直接执行default中的代码
* 注意:
* 1.当比较值与被比较值相等时进入程序,发现没有break时,
* 程序会向下执行碰到case时不判断直接向下执行直到break或者default出现
* 2.default是switch内最后执行的,即使是放在switch内的最上面,
* 不会报错可以执行,但是建议放在switch最下面
* */
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
//for语句嵌套switch使用
for(int x=0 ; x<5 ; x++){
//获取键盘录入数据
System.out.println("请输入数字(1-7天)");
int a = sc.nextInt();
//switch选择结构语句
//a是要比较的值
switch(a){
//case:后面跟的是要和表达式进行比较的值
case 1:
System.out.println("星期一");
//break:表示中断,结束的意思,可以控制switch语句的结束。
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期天");
break;
//default:当所有的值都和表达式不匹配的时候,
//就执行default控制的语句。其实它就相当于if语句的else。
default:
System.out.println("你输入的有误");
break;
}
}
五、do…switch
/**
* do...while循环的基本格式:
*
* 初始化语句;
* do {
* 循环体语句;
* }while(判断条件语句);
*
* do...while格式二:
*
* 初始化语句;
* do {
* 循环体语句;
* 控制条件语句;
* }while(判断条件语句);
*
* 循环语句的区别:
* do...while循环至少执行一次循环体。
* 而for,while循环必须先判断条件是否成立,然后决定是否执行循环体语句。
*
* */
int x = 0;
do {
//执行一次循环体
System.out.print("x");
//再进行判断
} while (x < 5);
int y = 0;
do{
System.out.print("y");
y++;
}while(y<5);