Java scanner知识

//用户交互Scanner 使用next方法
import java.util.Scanner;

public class Demo01 {
    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();
    }
}


import java.util.Scanner;

//使用nextLine方法
public class Demo02 {
    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();
    }
}

import java.util.Scanner;

//Easy is the best!
public class Demo03 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("用nextLine方法接收:");

        String str = scanner.nextLine();

        System.out.println("输出的内容为:" + str);

        scanner.close();
    }
}

import java.util.Scanner;

//输入多个数字 并求出其总和与平均数 每输入一个数字用回车确认 通过输入非数字来结束输入并输出执行结果
public class Demo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        double sum = 0;
        int n = 0;

        while (scanner.hasNextDouble()){
            sum += scanner.nextDouble();
            n += 1;
            System.out.println("输入第" + n +"个数,其总和为:" + sum);
        }

        System.out.println(n + "个数总和为:" + sum +" 其平均值为:" + sum/n);





        scanner.close();

    }
}

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

        System.out.println("你考试拿了哪个等级,可以得到相应的奖励:");

        char grade = scanner.next().charAt(0);

        switch (grade){
            case 'A':
                System.out.println("大豪斯");
                break;
            case 'B':
                System.out.println("公寓");
                break;
            case 'C':
                System.out.println("普通房");
                break;
            default:
                System.out.println("鸡笼");

        }

        scanner.close();
    }
}

//计算0到100之间到奇数和偶数的和
public class Demo06 {
    public static void main(String[] args) {
        int sumOdd = 0;
        int sumEven = 0;

        for (int i = 0; i <= 100; i++){
            if (i%2 != 0){
                sumOdd += i;
            }else{
                sumEven += i;
            }
        }
        System.out.println(sumOdd);
        System.out.println(sumEven);
    }
}

//用for循环输出1-1000之间能被5整除的数 并且每行输出3个
public class Demo07 {
    public static void main(String[] args) {

        for (int i =1; i <=1000; i++){
            if (i%5 == 0){
                System.out.print(i + "\t");
            }
            if (i%15 == 0){
                System.out.println();
            }
        }
    }
}

//打印99乘法表
public class Demo08 {
    public static void main(String[] args) {
        for (int i = 1; i<=9; i++){
            for (int j = 1; j <=i ; j++){
                System.out.print(i + "*" + j + "=" + (i*j) + "\t");
            }
            System.out.println();
        }
    }
}

//打印三角形 5行
public class Demo09 {
    public static void main(String[] args) {
        for (int i =1; i <=5; i++){
            for (int j = 5; j >= i; j--){
                System.out.print(" ");
            }
            for (int j =1; j <=i; j++){
                System.out.print("*");
            }
            for (int j =1; j <i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值