標籤:for 截取 一點 scale 轉換 line 優勢 res date
輸入:
格式1:Scanner sc = new Scanner(System.in);
格式2:Scanner sc = new Scanner(new BufferedInputStream(System.in));
資料量大時,格式2更快。
Split方法:
String str = scanner.next();
String[] date = str.split("/");
System.out.println(date[0]+"年"+date[1]+"月"+date[2]+"日");
DecimalFormat
NumberFormat
保留小數位元!!
BigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP);
子串:
String ss = "wo ai zhou";
System.out.println(ss.substring(3,7));
截取子串從第3個到第7個(從0開始,第7位不算)
結果是:ai z
高精度:
BigInteger和BigDecimal 是Java作為解題工具來說具有較大優勢的地方
BigInteger bigN = new BigInteger(st,base); //base表示進位
進位轉換
同樣也是Java非常具有優勢的一個地方
int a = Integer.parseInt("12534",8);
System.out.println(a);
String res = Integer.toString(12345,16);
System.out.println(res);
檔案輸入輸出
這裡只記錄比較高效的方法:
寫檔案:
FlieWriter類:
FileWriter fw = new FileWriter("my.txt");
fw.write("Hello");
fw.close();
讀檔案:
File file = new File("my.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isReader = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isReader);
String ss;
while((ss = br.readLine())!=null){
System.out.println(ss);
}
或者直接:
File file = new File("my.txt");
BufferedReader br =
new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String ss;
while((ss = br.readLine())!=null){
System.out.println(ss);
}
(我其實更喜歡這種,寫的時候不拖泥帶水)
Java一點輸入輸出技巧