键盘录入的基本格式

  import java.util.Scanner;
        
        class ScannerDemo {
            public static void main(String[] args)
            {
                Scanner sc = new Scanner(System.in); // 键盘录入对象
                System.out.println("输入一个整数");
                int x = sc.nextInt(); // 保存键盘录入的数据
                System.out.println(x);
            }
        }

switch语句的格式
      

 switch(表达式) {
              case 值1:      //基本数据类型可以接收byte,short,char,int
                    //引用数据类型可以接收枚举(JDK1.5)String字符串
                语句体1;
                break;
                case 值2:
                语句体2;
                break;
                …
                default:    
                语句体n+1;
                break;
        }

注意:
 * a:case后面只能是常量,不能是变量,而且,多个case后面的值不能出现相同的
    * b:default可以省略吗?
        * 可以省略,但是不建议,因为它的作用是对不正确的情况给出提示。
        * 特殊情况:
            * case就可以把值固定。
            * A,B,C,D
    * c:break可以省略吗?
        * 最后一个可以省略,其他最好不要省略
        * 会出现一个现象:case穿透。
        * 最终我们建议不要省略
    * d:default一定要在最后吗?
        * 不是,可以在任意位置。但是建议在最后。
    * e:switch语句的结束条件
        * a:遇到break就结束了
        * b:执行到switch的右大括号就结束了

switch语句和if语句的各自使用场景
*     switch建议判断固定值的时候用
*     if建议判断区间或范围的时候用

import java.util.Scanner;
class Test3_SwitchIf {
    public static void main(String[] args) {
        /*

        * 键盘录入月份,输出对应的季节
        一年有四季
        3,4,5春季
        6,7,8夏季
        9,10,11秋季
        12,1,2冬季
        */
        Scanner sc = new Scanner(System.in);    //创建键盘录入对象
        System.out.println("请输入月份");
        int month = sc.nextInt();                //将键盘录入的结果存储在month
        /*switch (month) {
        case 3:
        case 4:
        case 5:
            System.out.println(month + "月是春季");
        break;
        case 6:
        case 7:
        case 8:
            System.out.println(month + "月是夏季");
        break;
        case 9:
        case 10:
        case 11:
            System.out.println(month + "月是秋季");
        break;
        case 12:
        case 1:
        case 2:
            System.out.println(month + "月是冬季");
        break;
        default:
            System.out.println("对不起没有对应的季节");
        break;
        }*/

        //用if语句来完成月份对应季节
        if (month > 12 || month < 1) {
            System.out.println("对不起没有对应的季节");
        }else if (month >= 3 && month <= 5) {
            System.out.println(month + "月是春季");
        }else if (month >= 6 && month <= 8) {
            System.out.println(month + "月是夏季");
        }else if (month >= 9 && month <= 11) {
            System.out.println(month + "月是秋季");
        }else {
            System.out.println(month + "月是冬季");
        }
    }
}

 在控制台输出所有的”水仙花数”

    * 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
   

 for (int i =100;i<=999;i++)
        int ge = i%10
        int shi =i/10%10;
        int bai = i/100;
        if(ge*ge*ge+shi*shi*shi+bai*bai*bai==i)
        System.out.println(i);

循环结构三种循环语句的区别:
    * 三种循环语句的区别:
    * do...while循环至少执行一次循环体。
    * 而for,while循环必须先判断条件是否成立,然后决定是否执行循环体语句。
    * for循环和while循环的区别:
        * A:如果你想在循环结束后,继续使用控制条件的那个变量,用while循环,否则用for循环。不知道用谁就用for循
环。因为变量及早的从内存中消失,可以提高内存的使用效率。

两种最简单的死循环格式:
    * while(true){...}
    * for(;;){...}

在控制台输出九九乘法表:

class For99 {
    public static void main(String[] args)
    {
        for (int i=1;i<=9 ;i++ )
        {
            for (int j=1;j<=i ;j++ )
            {
                System.out.print(j+"*"+j+"="+i*j+"\t");
            }
            System.out.println();
        }
    }
}
/*
    注意:
        '\x' x表示任意,\是转义符号,这种做法叫转移字符。
        
        '\t'    tab键的位置
        '\r'    回车
        '\n'    换行
        '\"'
        '\''
*/

控制跳转语句break语句
* A:break的使用场景
    * 只能在switch和循环中

控制跳转语句continue语句
* A:continue的使用场景
    * 只能在循环中

控制跳转语句标号
* 标号:标记某个循环对其控制
* 标号组成规则:其实就是合法的标识符

class Demo_Mark {
    public static void main(String[] args)
    {
        outer:for (int i=1;i<=10 ;i++ )
        {
            System.out.println("i="+i);
            inner:for (int j=1;j<=10 ;j++ )
            {
                System.out.println("j="+j);
                break outer;  // 跳出outer
            }
        }

        System.out.print("baidu:");
        https://www.baidu.com/
        System.out.println("点击");
    }
}
/*
i=1
j=1
baidu:点击
*/
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-14
  • 2022-12-23
  • 2021-06-01
猜你喜欢
  • 2021-05-21
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
  • 2022-12-23
相关资源
相似解决方案