java基础第三天

本文讲解了Java包的命名规则、导入包的注意事项,介绍了Scanner类的next与nextLine操作,以及if、switch、while、for循环的使用,包括小数和整数输入、计算1-100和奇偶数和等实例。还涵盖了IDEL生成JavaDoc的方法和代码片段的演示。
摘要由CSDN通过智能技术生成

包机制

包语句的命名规则:package pkg1[.pkg2[.pkg3…]];

一般利用公司域名倒置作为包名,例如 www.baidu.com —>com .baidu.com
注意:

导入的包名和类名不要重复。

记住要去看阿里巴巴开发手册。

用Doc命令行生成自己的java帮助文档

步骤:包名右键 ->show in Explorer ->cmd打开->javadoc encoding UTF-8 -charset Explorer 文件名.java

如何用IDEL生成JavaDoc文档

链接点击查看详细方法


scanner详解

使用next输入

  public static void main(String[] args) {
        //创建一个扫描器对象,用于接受键盘数据
        Scanner scanner=new Scanner(System.in);
        System.out.println("使用next方式接收");
        //判断用户有没有输入字符串
        if(scanner.hasNext()){
          //使用next()方法接收
          String str=scanner.next();//程序会等待用户输入完毕,按回车结束
            System.out.println("输出的内容为:"+str);
        }
         //凡是属于IO流对的类如果不关闭会一张占用资源,养成习惯关闭
        scanner.close();

使用nextline输入

public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("使用nextLine方法接收:");
        if (scanner.hasNextLine()){
            String str=scanner.nextLine();
            System.out.println("输出的内容为:"+str);
        }
        scanner.close();
    }

next()和nextline()的区别:next一旦遇到空格就结束接收 ,而nextline结束的标志是Enter键 所以可以接收空格

小数和整数的输入输出

  public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int i=0;
        float f=0.0F;
        System.out.println("请输入整数:");
        if (scanner.hasNextInt()){
            i =scanner.nextInt();
            System.out.println("整数数据为:"+i);
        }else {
            System.out.println("你输入的不是整数!");
        }
        System.out.println("请输入小数:");
        if (scanner.hasNextFloat()){
            f=scanner.nextFloat();
            System.out.println("小数数据:"+f);
        }else {
            System.out.println("你输入的不是小数数据");
        }
    }

小案例

 public static void main(String[] args) {
        //我们可以输入多个数字,并求出和与平均数,
        // 每输入一个数按回车确认,通过输入非数字来结束
        Scanner scanner=new Scanner(System.in);
        double sum=0;//求和
        int m=0;//计算个数
        //通过while循环来接收数字,并在里卖弄统计数字的个数
        while (scanner.hasNextDouble()){
            double x=scanner.nextDouble();
            m+=1;
            sum+=x;
        }
        System.out.println(m+"个数的和为:"+sum);
        System.out.println(m+"个数的平均数为:"+(sum/m));

    }

if语句

 public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入内容:");
        String s=scanner.nextLine();
        //equals:判断字符串是否相等
        if (s.equals("hello")){
            System.out.println(s);
        }else {
            System.out.println("end");
        }
        scanner.close();
    }

if多分支语句

   if (score == 100) {
            System.out.println("你的成绩是满分");
        } else if (score < 100 && score >= 90) {
            System.out.println("你的成绩为A级");
        } else if (score < 90 && score >= 80) {
            System.out.println("你的成绩为B级");
        } else if (score < 80 && score >= 70) {
            System.out.println("你的成绩为C级");
        } else if (score < 70 && score >= 60) {
            System.out.println("你的成绩为D级");
        } else if (score < 60 && score >= 0) {
            System.out.println("你的成绩为不及格");
        }
        else {
            System.out.println("成绩不合法");
        }

switch语句

  public static void main(String[] args) {
        String name="林少";
      //字符串本质还是数字
      //反编译 java---class (字节码文件)---反编译(IDEL)
        switch (name){
            case "林少":
                System.out.println("大哥");break;
            case "小弟":
                System.out.println("dd");break;

            default:
                System.out.println("弄啥嘞");
        }
    }

注意:

swich里面的语句可以是byte,int ,short,char;

在javase7开始支持string类型;case标签必须为字符串常量或者字面量

while循环

 int i=0;
        while (i<100){
            i++;
            System.out.println(i);
        }//从1输到100
  while (true){
            //当遇到服务器等待连接或者监听信息 等等等 这些都需要死循环
        }

题目:计算1+2+3…+100?

    public static void main(String[] args) {
       /* int i=1;
        int sum=0;
        while (i<101){
            sum+=i;
            i++;
        }
        System.out.println(sum);*/
         int i=1;
        int sum=0;
        do {
            sum+=i;
            i++;
        }while (i<101);
        System.out.println(sum);
    }
/*while和do while的主要区别:
执行方式:while是先判断后执行,而dowhile是先执行后判断  所以dowhile最少也会执行一次循环*/
 

for循环

public static void main(String[] args) {
        int i=1;
        //while循环
        while (i<100){
            i+=2;
            System.out.println(i);
        }
        System.out.println("------------------------------------");
        //for循环
        for(int j=1;j<100;){
            j+=2;
            System.out.println(j);
        }
        //for 死循环
        for (;;){}
    }
    /*for循环有以下说明:
    最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个
    循环控制变量,也可以是空语句,然后,检测布尔表达式的值如果为true,循环体被执行,如果为false,循环终止,
    开始执行循环体后面的语句,再次检测布尔表达式,循环上面的过程
    * */

      //练习1:计算0-100之间奇数和偶数的和
        int oddsum=0;
        int evensum=0;
        for (int i = 0; i <= 100; i++) {
            if (i%2==0){//奇数
                oddsum+=i;
            }
            else {//偶数
                evensum+=i;
            }

        }
        System.out.println("奇数和:"+oddsum);
        System.out.println("偶数和:"+evensum);
    }
  //练习二:用while或for循环输出1-1000之间被5整除的数并且每行输出3个
        /*int i=0;
        int j=0;*/
  /*      while (i<1001){
            if(i%5==0){
                j++;
                System.out.print(i+"\t");
            }
            if (j>3){
                System.out.println("\n");j=0;}
                i++;
        }*/
        for (int i1 = 0; i1 < 1001; i1++) {
            if(i1%5==0){
                System.out.print(i1+"\t");
            }
            if(i1%(5*3)==0){
                System.out.println();
                //System.out.print("\n");
            }//print 输出完不会换行 println输出完会换行
        }

  //打印九九乘法表
        for (int i = 1; i < 10; i++)  {
            for(int j=1;j<=i;j++){
                System.out.print(i+"x"+j+"="+(i*j)+"\t");
            }
            System.out.println();
        }
 		//1.我们先打印第一列
        //2.把固定的1再用1个循环包起来
        //3.去掉重复项,j<=i
        //4.挑战样式 print \t

增强for循环

 public static void main(String[] args) {
        int []numbers={1,2,3,4,5};//定义一个数组
        for(int i=0;i<5;i++){
            System.out.println(numbers[i]);
        }
        System.out.println("-------------------");
        //遍历数组的元素 增强for
        for(int x:numbers){
            System.out.println(x);
    }
    }
两者遍历csdn上说并不等价,说增强for循环内不能更改集合长度

idea自动补全只有左括号,缺少右括号的问题解决

点击解决问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值