# 零基础学Java第三天

零基础学Java第三天

Scanner

Scanner是一个扫描器,用于扫描输入文本。

next()

next(): read the input only till the space. It can’t read two words separated by space. Also, next() places the cursor in the same line after reading the input.(next()只读空格之前的数据,并且cursor指向本行)
next() 方法遇见第一个有效字符(非空格,非换行符)时,开始扫描,当遇见第一个分隔符或结束符(空格或换行符)时,结束扫描,获取扫描到的内容,即获得第一个扫描到的不含空格、换行符的单个字符串。

文链接:https://blog.csdn.net/Megustas_JJC/article/details/68960433

nextInt()

nextInt(): it only reads the int value, nextInt() places the cursor(光标) in the same line after reading the input.(nextInt()只读取数值,剩下"\n"还没有读取,并将cursor放在本行中)

文链接:https://blog.csdn.net/Megustas_JJC/article/details/68960433

nextLine()

nextLine(): reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.
nextLine()时,则可以扫描到一行内容并作为一个字符串而被获取到。

文链接:https://blog.csdn.net/Megustas_JJC/article/details/68960433

代码

  • 1
public class Dome {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int i = 0;
        float f = 0.0F;
        System.out.println("请输入一个整数:");
        if (s.hasNextInt()){
            i = s.nextInt();
            System.out.println("你输入的数字为:"+i);
        }else{
            System.out.println("您输入的不是整数!");
        }

        System.out.println("请您输入一个小数");

        if (s.hasNextFloat()) {
            f = s.nextFloat();
            System.out.println("您输入的小数为:"+ f);
        }else{
            System.out.println("您输入的不是小数");
        }
        s.close();
    }
}

  • 2
public class Dome01 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) {
        // 创建一个Scanner对象,用于接收键盘数据
        Scanner s = new Scanner(System.in);
        System.out.println("使用next方法接收:");
        // 判断用户有没有输入字符串
        if (s.hasNext()){
            String str = s.next();
            System.out.println("输出的内容为"+str);
        }
        // 凡是IO流的类如果不关闭会一直占资源,养成好习惯用完就关掉
        s.close();

    }
}
  • 3
public class Dome02 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("使用nextLine方式接收:");
        if (s.hasNextLine()) {
            String str = s.nextLine();
            System.out.println("输出的内容为"+str);
        }
        s.close();

    }
}

  • 4
public class Dome03 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入内容:");
        String str = s.nextLine();
        System.out.println("输出的内容为:"+str);
        s.close();
    }

}

  • 5
public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("请输入数据:");
        int m = 0;
        double num = 0.0;
        while (s.hasNextDouble()) {
            double d = s.nextDouble();
            m++;
            num += d;
            System.out.println("你输入了"+m+"个数据"+"当前和为:"+ num);
        }

        System.out.println(m+"和为:"+ num);
        System.out.println(m+"平均数为:"+ (num / m));

    }
}

还学了个if

if是Java中的选择结构,Java中的基本结构是“顺序结构“

  • 语法

if(布尔表达式){

​ 结果为true就执行代码块

}

  • 代码
public class IfDome01 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入内容:");
        String str = s.nextLine();
        if (str.equals("hello!")){
            System.out.println(str);
        }
        System.out.println("End");
        s.close();
    }
}
public class IfDome02 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) {
        // 成绩大于60及格,小于不及格
        Scanner s = new Scanner(System.in);

        System.out.println("请输入成绩:");

        int score = s.nextInt();

        if (score >= 60){

            System.out.println("及格了,你真棒!");

        }else{

            System.out.println("这次考试你没有及格,下次加油哦!");
        }

        s.close();
    }
}

public class IfDome03 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入成绩:");

        double score = s.nextDouble();

        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("对不起,您输入的成绩不合法!");
        }
        s.close();
    }
}

public class IfDome04 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) {
        
        String name = "狂神";  // JDK7新特性增加了字符串
        switch ( name){ 
            case "狂神":
                System.out.println("11");
                break; // 不写会case穿透
            case "秦疆":
                System.out.println("22");
                break;
            case "kuangstudy":
                System.out.println("江湖");

        }
       
    }
}

完结撒花…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值