java中的输入输出格式控制

sc.next()sc.nextLine() 以回车为结束符

import java.util.Scanner;

public class test0101 {
    public static void main(String[] args) {
        System.out.print("输入:");

        Scanner sc = new Scanner(System.in);

        String s = sc.next();
        //String s = sc.nextLine();

        System.out.println("输出:" + s);
    }
}

java中的输入输出格式控制_第1张图片
sc.nextInt()以空格为结束符
若录入其他符号比如",""."会直接报错

import java.util.Scanner;

public class test0101 {
    public static void main(String[] args) {
        System.out.print("输入:");

        Scanner sc = new Scanner(System.in);

        int s = sc.nextInt();

        System.out.println("输出:" + s);
    }
}

在这里插入图片描述
sc.useDelimiter(",")+sc.nextInt()以逗号结束

import java.util.Scanner;

public class test0101 {
    public static void main(String[] args) {
        System.out.print("输入:");

        Scanner sc = new Scanner(System.in);

        sc.useDelimiter(",");

        int s = sc.nextInt();

        System.out.println("输出:" + s);
    }
}

在这里插入图片描述
sc.useDelimiter(",")+sc.next()以逗号结束

import java.util.Scanner;

public class test0101 {
    public static void main(String[] args) {
        System.out.print("输入:");

        Scanner sc = new Scanner(System.in);

        sc.useDelimiter(",");

        String s = sc.next();

        System.out.println("输出:" + s);
    }
}

在这里插入图片描述
sc.useDelimiter(",")+sc.nextLine()以回车结束

import java.util.Scanner;

public class test0101 {
    public static void main(String[] args) {
        System.out.print("输入:");

        Scanner sc = new Scanner(System.in);

        sc.useDelimiter(",");

        String s = sc.nextLine();

        System.out.println("输出:" + s);
    }
}

在这里插入图片描述

你可能感兴趣的:(java)