首页 > 技术文章 > 03 键盘录入,循环,方法

fly-book 2018-10-23 11:14 原文

键盘录入的基本格式

  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

return的作用
    * 返回
    * 其实它的作用不是结束循环的,而是结束方法的。
return和break以及continue的区别?
    * return是结束方法
    * break是跳出循环
    * continue是终止本次循环继续下次循环

方法:
/*
* A:为什么要有方法
    * 提高代码的复用性
* B:什么是方法
    * 完成特定功能的代码块。
* C:方法的格式
*
        修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...) {
            方法体语句;
            return 返回值;
        }
*/

class Demo_Sum {
    public static void main(String[] args)
    {

        int sum = add(10,20);
        System.out.println(sum); //30
    }
    public static int add(int a,int b){
        int sum = a + b;
        return sum;
    }

}


方法的注意事项
* A:方法调用(有具体返回值)
    * a:单独调用,一般来说没有意义,所以不推荐。
    * b:输出调用,但是不够好。因为我们可能需要针对结果进行进一步的操作。
    * c:赋值调用,推荐方案。
-------------------------------------
     a:方法不调用不执行
     b:方法与方法是平级关系,不能嵌套定义
     c:方法定义的时候参数之间用逗号隔开
     d:方法调用的时候不用在传递数据类型
     e:如果方法有明确的返回值,一定要有return带回一个值
* B:方法调用:(无返回值,void) 不写return;系统会默认加上
    * 单独调用
    * 输出调用(错误)
    * 赋值调用(错误)

方法重载:
    * 在同一个类中,方法名相同,参数列表不同。与返回值类型无关。
    
    * 参数列表不同:
        * A:参数个数不同
        * B:参数类型不同
        * C:参数的顺序不同(算重载,但是在开发中不用)

class Demo_Overload {
    public static void main(String[] args)
    {
        double s1 = add(1.1,2.2);
        System.out.println(s1); // 3.3000000000000003

        int s2 = add(10,20);
        System.out.println(s2); // 30

        int s3 = add(10,20,30);
        System.out.println(s3);  // 60
    }
    public static double add(double a,double b)
    {
        return a + b;
    }
    public static int add(int a,int b)
    {
        return a + b;
    }
    public static int add(int a,int b,int c)
    {
        return a + b + c;
    }
}

推荐阅读