Java学习第三天

java学习第三天

  • JavaDoc
    用来生成自己API文档的
    参数信息
    @author 作者名
    @version 版本号
    @since 指明需要最早使用的jdk版本
    @param 参数名
    @return 返回值情况
    @throws 异常抛出情况
package com.lixiaochuan.base;

/**
 * @author lixiaochuan  //给类加
 * @version 1.0
 * @since 1.8
 */
public class Doc {
    String name;

    /**
     * @author lixiaochuan
     * @param name
     * @return
     */
    public String test(String name){
        return name;
    }
}

Java流程控制

  • Scanner对象
    通过Scanner类来获取用户的输入。

基本语法
Scanner s = new Scanner(System.in);

通过Scanner类的next()与nextLine()方法获取输入的字符串,再读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。

next()
空格作为结束符
nextLine()
Enter为结束符

package com.lixiaochuan.scanner;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("使用nextLine方式接受输入:");
        //判断用户有没有输入字符串
        if (scanner.hasNextLine()){
            //使用nextLine方式接收
            String str = scanner.nextLine();
            System.out.println("输出的内容为:" + str);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,养成用完就关掉的习惯
        scanner.close();
    }
}
package com.lixiaochuan.scanner;

import java.util.Scanner;

public class Demo04 {
    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("输入的不是小数");
        }
        scanner.close();
    }
}
package com.lixiaochuan.scanner;

import java.util.Scanner;

public class Demo05 {
    public static void main(String[] args) {
        //输入多个数字,求和与平均数
        Scanner scanner = new Scanner(System.in);

        //和
        double sum = 0;
        //统计次数
        int m = 0;
        System.out.println("请输入数据:");
        //循环判断求和
        while (scanner.hasNextDouble()){
            double x = scanner.nextDouble();//依次输入数字
            m++;//统计次数
            sum = sum + x;
        }
        System.out.println("和为:"+sum);
        System.out.println("平均值:"+(sum/m));

        scanner.close();
    }
}
  • 顺序结构
    最基本的结构
  • 选择结构
    if~else结构
package com.lixiaochuan.structure;

import java.util.Scanner;

public class IfDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("收购是否成功?成功/失败");
        String  i = scanner.nextLine();

        if (i.equals("成功")){
            System.out.println("支付100万");
        }else {
            System.out.println("自己找人开发");
        }

        scanner.close();
    }
}
package com.lixiaochuan.structure;

import java.util.Scanner;

public class IfDemo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //输入成绩
        System.out.println("请输入成绩:");
        double score = scanner.nextDouble();
        //判断成绩
        if (score==100){
            System.out.println("恭喜满分!");
        }else if (score>=90&&score<100){
            System.out.println("优秀");
        }else if (score>=80&&score<90){
            System.out.println("良");
        }else if (score>=70&&score<80){
            System.out.println("不错");
        }else if (score>=60&&score<70){
            System.out.println("及格");
        }else if (score>=0 && score<60) {
            System.out.println("不及格");
        }else{
            System.out.println("成绩输入有误" );
        }

        scanner.close();
    }
}
  • switch多选择结构
    switch语句中的变量类型可以是:
    byte、short、int或者char,从JDK7开始,switch支持字符串String类型,同时case标签必须为字符串常量或字面量
package com.lixiaochuan.structure;

public class SwitchDemo03 {
    public static void main(String[] args) {
        //case  穿透现象  switch  匹配一个具体的值
        char grade = 'C';

        switch (grade){
            case 'A':
                System.out.println("优秀");
                break;//可选
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("及格");
                break;
            case 'D':
                System.out.println("再接再厉");
                break;
            case 'E':
                System.out.println("挂科");
                break;
            default:
                System.out.println("未知等级");
        }
    }
}
  • 循环结构
  1. while循环
    大多数情况是会让循环停止下来,需要一个让表达式失效的方式来结束循环。少部分情况需要循环一直执行,比如服务器的请求响应监听等。
package com.lixiaochuan.structure;

public class WhileDemo01 {
    public static void main(String[] args) {
        int i=0;
        //输出1~100
        while (i<100){
            i++;
            System.out.println(i);
        }
    }
}
package com.lixiaochuan.structure;

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

        //计算1+2+3+4~+100=?
        double sum = 0;
        int i = 0;

        while (i < 100) {
            i++;
            sum = sum + i;
        }
        System.out.println("1~100的和为:"+sum);
    }
}
  1. do…while循环
    有时候需要即使不满足条件,也至少执行一次
package com.lixiaochuan.structure;

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;

        do {
            i++;
            sum+=i;
        }while (i<100);
        System.out.println(sum);
    }
}
  1. for循环
    for循环语句支持迭代的一种通用结构,是最有效,最灵活的循环结构
package com.lixiaochuan.structure;

public class ForDemo01 {
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println(i);//使用100.for快速生成for循环语句
        }
    }
}

练习题
练习1:分别计算0到100之间的奇数和偶数的和

package com.lixiaochuan.structure;

public class ForDemo02 {
    public static void main(String[] args) {
        //练习1:分别计算0到100之间的奇数和偶数的和
        int oddSum = 0;
        int evenSum = 0;

        for (int i = 0; i <= 100; i++) {
            if (i%2==0)
                evenSum = evenSum+i;
            else
                oddSum = oddSum+i;
        }
        System.out.println("奇数之和:"+oddSum);
        System.out.println("偶数之和:"+evenSum);
    }
}

练习2:用while或for循环输出1-1000之间能被5整数的数,并且每行输出3个

package com.lixiaochuan.structure;

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

        int n = 0;
        for (int i = 0; i <= 1000; i++) {
            if (i % 5 == 0) {
                System.out.print(i+"\t");
                n++;
                if (n==3){
                    System.out.println();//实现换行
                    n=0;
                }
            }
        }
    }
}

练习3:打印99乘法表
1
1=1
12=2 22=4
13=3 23=6 33=9
1
4=4 24=8 34=12 44=16
1
5=5 25=10 35=15 45=20 55=25
16=6 26=12 36=18 46=24 56=30 66=36
17=7 27=14 37=21 47=28 57=35 67=42 77=49
1
8=8 28=16 38=24 48=32 58=40 68=48 78=56 88=64
1
9=9 29=18 39=27 49=36 59=45 69=54 79=63 89=72 99=81

package com.lixiaochuan.structure;

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

        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(i + "*" + j + "=" + (j * i)+"\t");
            }
            System.out.println();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值