1. 程式人生 > 其它 >java中的輸入輸出格式控制

java中的輸入輸出格式控制

技術標籤: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); } }

在這裡插入圖片描述
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);
    }
}

在這裡插入圖片描述