java中的流程控制:
if else
plus1:当if else里面只有一个语句的时候 {}可以不写
plus2:当if里面有return语句的时候 else单词可以不写
plus3:当if条件为true return true
当if条件为false return false
其实return回去的就是条件判断而已
plus4:不要拿着一个boolean类型的变量和true做连等比较
最终比完的结果和变量的值一模一样
switch case:
语法结构:
switch(参数){
case XXX : 执行语句;
case YYY : 执行语句;
case ZZZ : 执行语句;
}
学会利用break共享代码 将执行相同操作的代码放在一起
case:情况
switch:开关 改变
break:中止 中断
default:默认
面试题:
switch case的参数可以传哪些数据类型?
jdk1.0 char byte short int
jdk5.0 enum[枚举]
jdk7.0 String[字符串]
循环:
for
语法格式:
for(1;2;3){
4;
}
1:初始化循环变量
2:循环执行的条件
3:循环之后的变化
4:循环执行的代码
while:
1;
while(2){
4;
3;
}
do while:
语法格式:
1;
do{
4;
3;
}while(2);
eg:
int x = 1;
do{
System.out.println(x);
x++;
}while(x <= 100);
do while和while之间的区别》
while先判断 如何条件在执行
do while先执行 然后在判断 能保证程序至少走一次【先斩后奏】
1:测试 switch case
public class Exec1{
public static void main(String[] arfgs){
doSth("星期三");
}
//定义一个方法 显示每天做什么事
//星期一 打印:逛街
//星期二 看电影
//星期三 开例会
//星期四 看电影
//星期五 逛街
//星期六 开例会
//星期天 逛街
public static void doSth(String day){
switch(day){
case "星期一" :
case "星期五" :
case "星期天" : System.out.println("逛街");break;
case "星期二" :
case "星期四" : System.out.println("看电影");break;
case "星期三" :
case "星期六" : System.out.println("开例会");break;
default : System.out.println("参数错误");
}
//switch case break default
}
}
2:测试 switch case
public class Exec2{
public static void main(String[] args){
System.out.println(getColor(1));
}
/**
定义一个方法 显示球的颜色
1号球 返回:1号球黄色
2号球 2号球蓝色
3号球 3号球紫色
*/
public static String getColor(int number){
switch(number){
//return switch case break default
case 1 : return "1号球黄色";
case 2 : return "2号球蓝色";
case 3 : return "3号球粉色";
default : return "号码错误";
}
}
}
3:测试switch case
public class TestSwitchCase1{
public static void main(String[] args){
showSeason(15);
}
//定义一个方法 显示季节
//1-3:春 4-6:夏 7-9:秋 10-12:冬
public static void showSeason(int month){
switch(month){
case 1 :
case 2 :
case 3 : System.out.println("春");break;
case 4 :
case 5 :
case 6 : System.out.println("夏");break;
case 7 :
case 8 :
case 9 : System.out.println("秋");break;
case 10 :
case 11 :
case 12 : System.out.println("冬");break;
default : System.out.println("是不是傻");
}
}
}
1 : for循环
public class Exec3{
public static void main(String[] args){
//打印1-100
for(int x = 1;x <= 100;x++){
System.out.println(x);
}
//打印a-z
for(char x = 'a';x <= 'z';x++){
System.out.println(x);
}
for(char x = 97;x <= 122;x++){
System.out.println(x);
}
for(int x = 97;x <= 122;x++){
System.out.println((char)x);
}
}
}
2:for循环
//打印1-100之间所有的8的倍数
public class Exec4{
public static void main(String[] atgs){
//先把1-100之间所有的数字得到 -》 for
for(int x = 1;x <= 100;x++){
//x -> 数字
//每次循环对一个数字判断是不是8的倍数 如果是 打印出来
if(x % 8 == 0){
//System.out.println(x);
}
}
for(int x = 8;x <= 100;x += 8){//8 9/16
System.out.println(x);
}
}
}
public class Exec5{
public static void main(String[] args){
//统计1-100之间有几个8的倍数
int y = 0;
//先得到1-100之间所有的数字
for(int x = 1;x <= 100;x++){
//判断这个数字是不是8的倍数
if(x % 8 == 0){
//如果是的话 统计个数 变量++
y++;
}
}
System.out.println(y);
//System.out.println(100 / 8);//12
//统计1+2+3+4....+100的和
int sum = 0;
for(int x = 1;x <= 100;x++){
//x-> 数字
//sum = 0 + 1 + 2 + 3....
sum = sum + x;//sum += x;
}
System.out.println(sum);
System.out.println((1 + 100) * 100 / 2);
}
}
public class Exec6{
public static void main(String[] args){
//求1-100之间有几个5倍数
int y = 0;
for(int x = 1;x <= 100;x++){
if(x % 5 == 0){
//x是5的倍数 统计个数 ++
y++;
}
}
System.out.println(y);
int count = 0;
for(int x = 5;x <= 100;x+=5){
count++;
}
System.out.println(count);
//统计2+4+6+8+10.。。。100的和
int sum = 0;
for(int x = 1;x <= 100;x++){
if(x % 2 == 0){
//x->偶数
sum = sum + x;
}
}
int sum1 = 0;
for(int x = 2;x <= 100;x+=2){
//x -> 偶数
sum1 = sum1 + x;
}
System.out.println(sum1);
}
}
3:while循环
public class Exec7{
public static void main(String[] args){
}
//定义一个方法 得到>=x的最小的2的n次方数
public static void getValue(int x){//5
//x = 3 4
//x = 4 4
//x = 9 16
//x = 25 32
//x = 64 64
int y = 1;//2(n)
//y = 1 -> 2 -> 4 -> 8
//1 < 5
//2 < 5
while(y < x){
y = y * 2;//y <<= 1; y = y << 1
}
System.out.println(y);
}
}
/**
x:传进去的数值
y=1 -> 2(n)
y < x -> 继续y*2
打印y:
int x = 9;
y = 1;
1 < 9 y*2 = 2
2 < 9 y*2 = 4
4 < 9 y*2 = 8
8 < 9 y * 2 = 16
16 < 9 -> 16
1 2 4 8 16 32 64 128 256.....2(n)
1*2*2*2*2*2
*/
while循环:打印第一个不符合条件的数值
public class TestWhile1{
public static void main(String[] args){
//打印1-10
int x = 1;//1
while(x <= 10){//2
System.out.println(x);//4
x++;//3
}//x消亡
System.out.println("第一个不符合条件的x:" + x);
//a-z
char y = 'a';
while(y <= 'z'){
System.out.println(y);
y++;
}
}
}