JAVA_Day_04(switch,for,while;水仙花数,九九表,珠穆朗玛峰;break,continue,return)

1:switch语句的格式?针对格式的解释?以及注意事项?

       A:格式:

              switch(表达式) {

                     case值1:

                            语句体1;

                            break;

                     case值2:

                            语句体2;

                            break;

                     ...

                     default:

                            语句体n+1;

                            break;

              }

       B:格式解释说明:

                     switch:说明这是switch语句。

                     表达式:可以是byte,short,int,char

                            JDK5以后可以是枚举

                            JDK7以后可以是字符串

                     case:后面的值就是要和表达式进行比较的值

                     break:表示程序到这里中断,跳出switch语句

                     default:如果所有的情况都不匹配,就执行这里,相当于if语句中的else

       C:注意事项:

              A:case后面只能是常量,不能是变量,而且,多个case后面的值不能出现相同的

              B:default可以省略吗?

                     可以省略,但是不建议,因为它的作用是对不正确的情况给出提示。

                     特殊情况:

                            case就可以把值固定。

                            A,B,C,D

              C:break可以省略吗?

                     可以省略,但是结果可能不是我们想要的。

                     会出现一个现象:case穿透。

                     最终我们建议不要省略

              D:default一定要在最后吗?

                     不是,可以在任意位置。但是建议在最后。

              E:switch语句的结束条件

                     a:遇到break就结束了

                     b:执行到末尾就结束了

2:看程序,分析下面程序的结果:

int x = 2,y=3;
	switch(x)
	{
   	           default:
  	                   y++;              //无符合选项执行y++,
  	            case 3:            //符合这个case,进去执行,break跳出
 	                    y++;
 	                    break;
 	             case 4:
          	           y++;
	}           
	System.out.println("y="+y);         //y=5

 

3:根据输入的值,判断是星期几。(分别用if语句和switch语句实现)

       提示:输入:1    

                     输出:星期1

 

/*
*========================================================
 * Program: If_Else_Or_Switch
 * This Based on the input values, the judgment ofthe
*   week.(respectively with ifstatements and switch
*   statement)
 *    About:
 * Date:2017/06/03
 * @author: Bruce_You173428680@qq.com
 * @version:  1.0
 *========================================================
*/
package cn.incast_01;
 
import java.util.Scanner;
 
public class If_Else_Or_Switch {
 
   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generatedmethod stub
     
      //Create a keyboard entryobject.
      Scanner sc = new Scanner(System.in);
     
      //Control the keyboard inputdate. 
      System.out.println("Pleaseenter a number (1-7), there will be corresponding week:");
      int week = sc.nextInt();
     
      //Switch judgment statement
      switch (week) {
      case 1:
        System.out.println("This isMonday.");
        break;
      case 2:
        System.out.println("This isTuseday.");
        break;
      case 3:
        System.out.println("This isWednesday.");
        break;
      case 4:
        System.out.println("This isThursday.");
        break;
      case 5:
        System.out.println("This isFriday.");
        break;
      case 6:
        System.out.println("This isSaturday.");
        break;
      case 7:
        System.out.println("This isSunday.");
        break;
      default:
        System.out.println("Pleaseenter a correct date.");
        break;
      }
      System.out.println("The aboveis switch.");
     
     
      //
      System.out.println();
      System.out.println("Pleaseenter a number (1-7), there will be corresponding week:");
      int week1 = sc.nextInt();
     
      //(if else) judgmentstatement
      if (week1 < 1 || week1 >7) {
        System.out.println("Pleaseenter a correct date.");
      } else if (week1 == 1){
        System.out.println("This isMonday.");
      } else if (week1 == 2){
        System.out.println("This isTuseday.");
      } else if (week1 == 3){
        System.out.println("This isWednesday.");
      } else if (week1 == 4){
        System.out.println("This isThursday.");
      } else if (week1 == 5){
        System.out.println("This isFriday.");
      } else if (week1 == 6){
        System.out.println("This isSaturday.");
      } else if (week1 == 7){
        System.out.println("This isSunday.");
      }
      System.out.println("The aboveis (if else).");
     
      System.out.println();
      System.out.println("Theauthor:");
      System.out.println("Bruce_You");
     
     
   }
 
}
 
/*
*########################################################
* ############# End OfIf_Else_Or_Switch ################
*########################################################
*/

 

 

 

4:for循环的格式?要能看懂执行流程。

  用for循环完成如下案例

 

  求和

//定义第一个加数
              intsum = 0;
              for(intx=1; x<=10; x++) {
                     //这里的x其实是第二个加数
                     sum= sum + x;
                     /*
                            0+ 1 = 1
                                          1+ 2 = 3
                                                        3+ 3 = 6
                                                        ...
                     */
                     //sum+= x;
              }
              System.out.println("sum:"+sum);

  求偶数和

int sum2 = 0;
              for(intx=1; x<=100; x++) {
                     if(x%2== 0) {
                            sum2+= x;
                     }
              }
              System.out.println("1-100偶数之和是:"+sum2);
              System.out.println("------------------");

  求奇数和

int sum3 = 0;
             
              for(intx=1; x<=100; x+=2) {
                            sum3+= x;
              }
             
              System.out.println("1-100奇数之和是:"+sum3);
              System.out.println("------------------");
       }

  打印水仙花数

           

   //三位数其实是告诉了我们范围。
      for(intx=100; x<1000; x++) {
       int ge = x%10;
       int shi = x/10%10;
       int bai = x/10/10%10;
                    
        //让ge*ge*ge+shi*shi*shi+bai*bai*bai和该数据比较
        if(x== (ge*ge*ge+shi*shi*shi+bai*bai*bai)) {
        //如果相同,就把该数据在控制台输出。
        System.out.println(x);

  统计水仙花数

              //定义统计变量,初始化值是0
              intcount = 0;
             
              //三位数告诉了我们范围,用for循环就可以搞定
              for(intx=100; x<1000; x++) {
                     //获取每一个三位数的个,十,百的数据
                     intge = x%10;
                     int shi = x/10%10;
                     intbai = x/10/10%10;
                    
                     //按照要求进行判断
                     if(x== (ge*ge*ge+shi*shi*shi+bai*bai*bai)) {
                            //如果满足要求就计数。
                            count++;
                     }
              }
             
              System.out.println("水仙花数共有"+count+"个");
    

  九九乘法表

class Multiplication_Table {

       publicstatic void main(String[] args) {

              //为了使用数据,我们从1开始

              for(intx=1; x<=9; x++) {

                     for(inty=1; y<=x; y++) {

                            System.out.print(y+"*"+x+"="+y*x+"\t");

                     }

                     System.out.println();

              }

       }

}


 

5:while循环的格式?要能看懂执行流程

  用while循环完成如下案例

 

  求和

int sum2 = 0;
              inty=1;
              while(y<=100){
                     sum2+=y;
                     y++;
              }
             
              System.out.println("sum2:"+sum2);

  纸张折叠成珠穆朗玛峰高度的次数

class While_Ci {
       publicstatic void main(String[] args) {
              //定义一个统计变量,默认值是0
              intcount = 0;
             
              //最高山峰是珠穆朗玛峰:8848m这是最终的厚度
              //我现在有一张足够大的纸张,厚度为:0.01m这是初始厚度
              //为了简单,我把0.01变成1,同理8848就变成了884800
              intend = 884800;
              intstart = 1;
 
              while(start<end){
                     //只要每次变化的厚度没有超过珠穆朗玛峰的高度,就折叠,统计变量++
                     count++;
                    
                     //折叠一次有什么变化呢?就是厚度是以前的2倍。
                     start*= 2;
                    
                     System.out.println("第"+count+"次厚度是"+start);
              }
             
              //输出统计变量。
              System.out.println("要叠"+count+"次");
       }
}

6:break,continue和return分别有什么用?

(1)break:中断的意思

              A:用在循环和switch语句中,离开此应用场景无意义。

              B:作用

                     a:跳出单层循环

                     b:跳出多层循环,需要标签语句的配合

        (2)continue:继续

              A:用在循环中,离开此应用场景无意义。

              B:作用

                     a:跳出单层循环的一次,可以继续下一次

              C:填空题

                     for(intx=1; x<=10; x++) {

                            if(x%3== 0) {

                                   //补齐代码

                            }

                            System.out.println("Java基础班");

                     }

                     如何让控制台输出2次:Java基础班

                     如何让控制台输出7次:Java基础班

                     如何让控制台输出13次:Java基础班

       (3)return:返回

              A:用于结束方法的,后面还会在继续讲解和使用。

              B:一旦遇到return,程序就不会在继续往后执行。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值