JAVA---程序运行顺序(练习)

目录

5 程序的运行顺序

5.1分支结构

(1)if分支结构

(2)switch-case语句

5.2循环结构     

(1)for循环

(2)while循环

(3)do-while循环


5 程序的运行顺序

5.1分支结构

(1)if分支结构

if:

public class IfDemo01{
	public static void main(String[] args){
	    int a = 10;
		int b = a*2;
		if(a==b){
		    System.out.println("a==b");
		}
		if(a!=b){
		    System.out.println("a!=b"); 
		}
		boolean f=false;
		if(!f){
            System.out.println("i like you");
		}
		if(!(a>b)){
		    System.out.println("!(a>b)");
		}
		    System.out.println("over");
	}
}

if-else:

public class IfDemo02{
    public static void main(String[] args){
	    int a = 10;
		int b = 5;
		if(a>=b){
		    System.out.println("a>=b");
		}else{
		    System.out.println("a<b");
		}
	    if(a==b){
		    System.out.println("you are right");
		}else{
		    System.out.println("I'm right");
		}
        char gender = '男';
		int  age = 21;
		if(gender=='男'&&age>20){
            System.out.println("此男已经达到法定结婚年龄");
		}else{
			if(gender=='女'&&age>=18){
			     System.out.println("此女已经达到法定结婚年龄");
			}else{
			     System.out.println("此人没有达到法定结婚条件");
			}
		}
		System.out.println("over!!!");
	}
}

if-else if :

/**
    多条件分支语句;
	if-else if 写法
*/
public class IfDemo03{
	public static void main(String[] args){
	    int a = 2;
		int b = 2;
		if(a>b){
		    System.out.println("a>b");
		}else if(a==b){
		    System.out.println("a==b");
		}else{
		    System.out.println("a<b");
		}
		 System.out.println("over");
	}
}

(2)switch-case语句

/**分支结构之switch-case语句

    switch(表达式){
    case value1:代码块;[break;]
    case value2:代码块;[break;]
    ……
    [default:代码块n;]
   }
   表达式:
       可以是整数表达式
	   jdk1.7版本后,支持字符串表达式

*/
public class SwitchDemo01{
    public static void main(String[] args){
	    int a = 4;
		int b = 11;
		switch(b%a){
		case 0:
		    System.out.println("b是a的整数倍");
		    break;
		case 1:
            System.out.println("b对a取余结果为1");
		    break;
		case 2:
			System.out.println("如果b再+1,就是a的整数倍");
		}
		System.out.println("over");

		int x = 4;
		int y = 11;
		switch(y/x){
		case 1:
			System.out.println("A");
		case 2:
			System.out.println("B");
		case 3:
			System.out.println("C");
		    break;
		default:
			System.out.println("D");
		}
	    System.out.println("end");
	}
}   
public class SwitchDemo02{
	public static void main(String[] args){
	    String str = "A";
		switch(str){
		case "A":
			System.out.println("AAAAA");
		    break;
		case "B":
            System.out.println("BBBBB");
		    break;
		case "C":
			System.out.println("CCCCC");
		    break;
		case "abc":
		    System.out.println("hhhhh");
		}
		System.out.println("over");
	}
}
public class TestDemo01{
	public static void main(String[] args){
	    /*
		  练习1:定义一个变量,用来存储月份
		         使用switch-case语句输出此月份在第一季度,
				 第二季度,第三季度,第四季度的哪一个季度
          练习2:操场上有m个人,三人一组,问余下几人
		  练习3:使用switch-case语言修改成绩等级的练习
		         小明   score
				 100--S   90~99--A  80~89--B  70~79--C  60~69--D   <59  E

		*/
	     //练习1:第一种
		 int month = 1;
		 switch((month-1)/3){
		 case 0:             
			 System.out.println(month+"月是第一季度");
		     break;
		 case 1:
		     System.out.println(month+"月是第二季度");
		     break;
		 case 2:
             System.out.println(month+"月是第三季度");
		     break;
		 default:
             System.out.println(month+"月是第四季度");
		 }
		 
		 //练习1:第二种,老师演示
		 switch(month){
			 case 1:
			 case 2:
			 case 3:
                 System.out.println(month+"月是第一季度");
		         break;
			 case 4:
			 case 5:
			 case 6:
                 System.out.println(month+"月是第二季度");
		         break;
			 case 7:
			 case 8:
			 case 9:
                 System.out.println(month+"月是第三季度");
		         break;
			 case 10:
			 case 11:
			 case 12:
                 System.out.println(month+"月是第四季度");
		         break;
		 }
		 //练习1:第三种
         int t = 0;
         String m = "0";		 
		 switch((month-1)/6){
		 case 0:             
			 t = (month-1)/3;
			 m = t==0?"第一季度":"第二季度";
		     System.out.println(m);
		     break;
		 case 1:
             t = (month-1)/3;
			 m = t==2?"第三季度":"第四季度";
		     System.out.println(m);
		     break;
		 }	  
		 //练习2
		 int a = 40;
		 switch(a%3){
		 case 0:
             System.out.println("余下0人");
		     break;
		 case 1:
             System.out.println("余下1人");
		     break;
		 default:
             System.out.println("余下2人");
		 }
		 int b = 78;
		 switch(b/10){
	     case 10:
             System.out.println("S");break;
         case 9:
			 System.out.println("A");break;		     
         case 8:
			 System.out.println("B");break;		     		
		 case 7:
			 System.out.println("C");break;		     
		 case 6:
             System.out.println("D");break;		     		 
		 default:
             System.out.println("E");	     
		 }			 
	}
}

5.2循环结构     

(1)for循环

public class TestDemo02{
	public static void main(String[] args){
	   /*计算1到100的整数之和,扣去50和55*/
	   /*计算1~15的整数乘积  扣去4的倍数*/
	   /*随机出10道乘数是10以内的整数的加法题*/
       int sum = 0;
	   for (int i = 1;i<=100 ;i++ ){
		   if (i == 50|| i == 55){
			   continue;
		   }
		       sum = sum +i;		   
	   }
	   System.out.println(sum);
	   //老师演示
	   int s = 0;
	   for (int i = 1;i<=100 ;i++){
		   if (i != 50&& i !=55){
               s += i;
		   }
	   }
	  System.out.println(s);

	  long t = 1;
	  for (long i = 1;i<=15 ;i++ ){
		  if(i%4==0){
		      continue;
		  }
		  t = t*i;		  
	  }
	  System.out.println(t); 

	  for (int i = 0;i<10 ;i++ ){
           int a = (int)(Math.random()*10);
		   int b = (int)(Math.random()*10);
		   System.out.println(a+"+"+b+"=");
	  }
	}
}
/**
   素数:也叫质数,只能被1和本身整除的数
   1不是素数
   随机一个区间[2,100]的任意整数,判读是不是素数
*/
public class TestDemo03{
	public static void main(String[] args){
        int a = (int)(Math.random()*99+2);
         //定义一个变量充当标记	
		boolean label = false;
		int i = 2;
		for (i = 2;i<a-1 ;i++ ){            
			if (a%i==0){
			 label = true;	/*
			                  能被i整除法,说明不是素数,改变一下标价状态,
			                  没有必要再判断其他除数了,所以break结束循环结构
							 */		 
				break;
            }
        }
		/*
		  查看标记label是否改变状态
		*/
		if(!label){
		    System.out.println(a+"是素数");
		}else{
		    System.out.println(a+"不是素数");
		}
	}
}
/**
   打印直角三角形
   *
   **
   ***
   ****
   *****
*/
public class TestDemo04{
	public static void main(String[] args){
		for (int i = 0;i<5 ;i++ ){
             for (int j = 0;j<i+1 ;j++ ){
                  System.out.print("*");
			 }
			System.out.println();
		}
	}
}
/**
练习
1、打印1到100以内的所有素数,每10个数一行
2、打印等腰三角形五行
     *
	***
   *****
  *******
 *********
3、打印菱形
     *
	***
   *****
  *******
 *********
  *******
   *****
    ***
	 *

*/
public class TestDemo08{
	public static void main(String[] args){	
		 int count = 0;
		 for (int i = 2;i<101 ;i++ ){	         //练习1		 
			  boolean label = true;
			  for (int a = 2; a<i;a++ ){
				   if (i%a==0){
					    label = false;
					    break;
			       }				
		      }
			  if (label){
                   count++;
				   if (count%10!=0){
				        System.out.print(i+"\t");
				   }else{
				        System.out.print(i+"\t");
					    System.out.println();
			       }			  					
		      }	
	     }
	     System.out.println();
		
		 for (int L = 1;L<=5 ;L++ ){              //练习2
			 for (int r = 1;r<=9 ;r++ ){
				  if (r<=5-L){
					  System.out.print(" ");
				  }else if (r>=5+L){
                      System.out.print(" ");
				  }else{
				      System.out.print("*");
				  }				  
			 }
			 System.out.println();
		 }
	/*
	   老师演示:   行        打星的列
     *              i=0         j=5     
	***             i=1         j=4 5 6
   *****            i=2         j=3 4 5 6 7
  *******           i=3         j=2 3 4 5 6 7
 *********          i=4         j=1 2 3 4 5 6 7 8 9
         开始打*的临界点
		 j=5-i
		 结束打*的临界点
		 j=5+i
		 所以打*的范围是:5-i《j《5+i
思路二:
                    行          *的个数
     *              i=1          1     
	***             i=2          3
   *****            i=3          5
  *******           i=4          7
 *********          i=5          9
         *的个数与行号的关系
		   count=2*i-1;	
		   打印空格的关系:
		   j<=5-i
	*/	
         for (int i = 1;i<6 ;i++ ){
		      for (int j = 1;j<6-i ;j++ )
		      {
				  System.out.print(" ");
		      }
			  for (int j = 0;j<2*i-1 ;j++ ){
				  System.out.print("*");
			  }
			  System.out.println();
         }

/*3、打印菱形
     *                      i=1     *1       j=5
	***                     i=2     *3       j=4 5 6
   *****                    i=3     *5       j=3 4 5 6 7
  *******                   i=4     *7       j=2 3 4 5 6 7 8                 
 *********                  i=5     *9       j=1 2 3 4 5 6 7 8 9
  *******                   i=6     *7       j=2 3 4 5 6 7 8
   *****                    i=7     *5       j=3 4 5 6 7
    ***                     i=8     *3       j=4 5 6
	 *                      i=9     *1       j=5

	 前五行,打*的范围6-i~4+i
	 后四行,打*的范围i-4~14-i

*/
		
		 for (int L = 1;L<=9 ;L++ ){              //练习3
			 if (L<=5){			 
			     for (int r = 1;r<=9 ;r++ ){
				      if (r<=4+L&&r>=6-L){
					      System.out.print("*");
				      }else{
					      System.out.print(" ");
					  }			  
			     }
				     System.out.println();
		         }else{
			          for (int l = 1;l<=9 ;l++ ){
					       if (l<=14-L&&l>=L-4){
                               System.out.print("*");
					       }else{
						       System.out.print(" ");
						   }
				      }
			          System.out.println();
			     }
		 }
		 //方法二:从第i行前五行是有2i-1个*,从第6行开始是有19-2i个*
		 for (int i=1;i<=9;i++){
			 if (i<=5){
				 for (int j = 1;j<=5-i ;j++){
					  System.out.print(" ");
				 }
				 for (int j = 1;j<=2*i-1;j++){
					  System.out.print("*");
				 }
			 }else{
			     for (int j = 1;j<i-4;j++){
                      System.out.print(" ");
			     }
				 for (int j = 1;j<=19-2*i;j++ ){
                      System.out.print("*");
				 }			 
			 }
			 System.out.println();
		 }
	}	
}

(2)while循环

/**
   while循环结构:
     while(循环条件){
	     循环体
	 }

*/
public class WhileDemo01{
	public static void main(String[] args){
		//打印10次hello world
		int count = 1;
		while(count<=10){
		    System.out.println(count+":热巴");
			count++;
		}

		//第二种
		int num = 1;
		while(true){
		   if (num<=10){
		       System.out.println("热巴");
			   
		   }else{
		       break;
		   }
		   num++;
		}
		//第三种
		int n = 1;
		while(true){
		   System.out.println("热巴");
           n++;
		   if (n>10){    
			   break;
		   }
		}
	}
}

(3)do-while循环

/**
  do-while循环结构:
      do{
	     循环体
	  }while(循环条件);
*/
import java.util.Scanner;
public class DoWhileDemo01{
	public static void main(String[] args){
       /*银行卡密码为888888
	     使用do-while模拟ATM输入密码操作	   
	   */ 
	  Scanner sc = new Scanner(System.in);
	  int input = -1;
	 /* do{
	      System.out.println("请输入密码");
		  input = sc.nextInt();
		  if (input==888888){
			  break;
		  }
	  }while(true);
      System.out.println("密码验证成功");
	}*/

	//第二种写法:输入密码直到成功结束
         do{
	      System.out.println("请输入密码");
		  input = sc.nextInt();
		  }while(input!=888888);
		  System.out.println("密码验证成功");
		  
    }
}

 Scanner: 

/**
  引用类型之Scanner类的学习
    Scanner:开启键盘输出。
*/
import java.util.Scanner;
public class ScannerDemo{
	public static void main(String[] args){
	    Scanner sc = new Scanner(System.in);
		int n1 = sc.nextInt();//程序阻塞程序
		int c1 = 5;
		int sum = n1+c1;
		System.out.println("sum:"+sum);
	}
    
}

猜你喜欢

转载自blog.csdn.net/zoe_ranxiaosu/article/details/81259109
今日推荐