Java基础:程序控制语句

示例代码:

  1. class Demo{                                                    //类名Demo
  2.     public static void main(String args[])throws Exception{  //抛出异常
  3.         char ch;
  4.         System.out.println("Press a key followed by Enter:");  //打印信息
  5.         ch=(char)System.in.read();                      //从键盘输入system.in.read();
  6.         System.out.println("Your key is:"+ch);    //打印输入的字符
  7.         }
  8. ------------------------------------------------------------------------------------------------------------
  9. if else if 以及嵌套        
  10.         int i;
  11.         for(i=0;i<6;i++){
  12.             if(i==1){
  13.                 System.out.println("i is one");
  14.                 }else if(i==2){
  15.                     System.out.println("i is two");
  16.                     }else if(i==3){
  17.                         System.out.println("i is three");
  18.                         }else if(i==4){
  19.                             System.out.println("i is four");
  20.                         }
  21.                             else{
  22.                             System.out.println("i is not between one to four");
  23.                             }
  24.             
  25.             }
  26. ------------------------------------------------------------------------------------------------------------
  27. for循环
  28.         int fact=1;
  29.         int i;
  30.         int sum=0;
  31.         for(i=1;i<=5;i++){
  32.             sum+=i;               //1-5的和
  33.             fact*=i;                //1-5的阶乘 
  34.             
  35.             System.out.println("sum="+sum);
  36.             System.out.println("fact="+fact);
  37.             System.out.println();
  38.             }
  39.         System.out.println(i);
  40.         }
  41. -----------------------------------------------------------------------------------------------------------
  42. While循环
  43.         char ch='a';
  44.         while(ch<='z'){
  45.             System.out.println(ch);
  46.             ch++;
  47.             }
  48. ----------------------------------------------------------------------------------------------------------
  49. do while循环
  50.         char ch;
  51.         int count=0;
  52.         System.out.println("请输入字符串,以句号结束");
  53.         do{
  54.             ch=(char)System.in.read();
  55.             
  56.             if(ch==' ') count++;
  57.             
  58.         }while(ch!='.');
  59.          System.out.println("count"+count);    
  60. ---------------------------------------------------------------------------------------------------------
  61. switch语句
  62.     int i;
  63.     for(i=0;i<10;i++){
  64.         switch(i){
  65.             case 0:
  66.                 System.out.println("i is zero");
  67.                 break;                                      //break终止循环
  68.                                                                  //continue结束本次循环继续下次循环
  69.                 
  70.             case 1:
  71.                 System.out.println("i is one");
  72.                 break;
  73.                     
  74.             case 2:
  75.                 System.out.println("i is two");
  76.                 break;
  77.                    
  78.             case 3:
  79.                 System.out.println("i is three");
  80.                 break;
  81.                 
  82.             case 4:
  83.                 System.out.println("i is four");                
  84.                 break;                
  85.             default:{
  86.                 System.out.println("i is five or more");
  87.                 }
  88.             }
  89.         }
  90.     
  91. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值