java中的輸入輸出格式控制
阿新 • • 發佈:2021-02-13
技術標籤: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);
}
}