Core Java (三) Java的輸入與輸出

來源:互聯網
上載者:User

一個簡單的java輸入輸出執行個體,包含了最常用的輸入輸出函數。

package com.xujin;import java.util.Date;import java.util.Scanner;public class MyTest{static Scanner cin = new Scanner(System.in);public static void main(String[] args){System.out.print("input your name:");String name = cin.next();//讀入下一個單詞,以空格作為標準System.out.print("input your age:");int age = cin.nextInt();//讀入下一個int,以空格作為標準System.out.print("input your salary:");double salary = cin.nextDouble();System.out.print("input detail:");String detail = cin.nextLine();//讀入下一行,這裡是讀入上一次的斷行符號detail = cin.nextLine();//讀入下一行//格式化輸出System.out.printf("Name:%s\n", name);System.out.printf("Age:%d\tOct:%<o\tHex:%1$#x\n",age);//output:Age:20Oct:24Hex:0x14System.out.printf("Salary:%#,8.2f\nDetail:",salary);System.out.println(detail);//時間輸出System.out.printf("Now:%tc\n",new Date());//output:星期三 一月 16 21:02:10 CST 2013System.out.printf("%1$s %2$tY %2$tB %2$td\n", "Now:",new Date());//output:2013 一月 16System.out.printf("%s %tY %<tB %<td\n", "Now:",new Date());//output:2013 一月 16//使用靜態String.format方法常見一個格式化的字串,而不列印輸出System.out.println(detail);String message = String.format("Hello,%s! You will be %d next year", name, ++age);System.out.print(message);}}

console結果:

input your name:Gin
input your age:20
input your salary:5000
input detail:detail...
Name:Gin
Age:20 Oct:24
Hex:0x14
Salary:5,000.00
Detail:detail...
Now:星期三 一月 16 21:12:38 CST 2013
Now: 2013 一月 16
Now: 2013 一月 16
detail...

Hello,Gin! You will be 21 next year

解釋:

System.out.printf("Age:%d\tOct:%<o\tHex:%1$#x\n",age);//output:Age:20Oct:24Hex:0x14

%<o表示格式化前面說明的數值,以八進位顯示age
%1$#x表示格式化索引為1(1$,第一個參數,從1開始)的數值,並填加首碼0x或0(#對x或o)

System.out.printf("Salary:%#,8.2f\nDetail:",salary);

包含小數點(#對f),8.2表示用8個字元寬度和小數點後兩個字元的精度列印

Java的格式輸出,,在%後面使用

用於printf的轉換符
轉換符 類型
d 十進位整數
x 十六進位整數
o 八進位整數
f 定點浮點數
e 指數浮點數
g 通用浮點數
a 十六進位浮點數
s 字串
c 字元
b 布爾
h 散列碼
tx 日期時間
% 百分比符號
n 與平台有關的行分隔字元

用於printf的標誌,在%後面使用

  

標誌

目的

舉例

+

列印數字前的符號

+3333.33

space 

在正數之前加空格

|  3333.33|

0

在數字前補0

003333.33

-

 靠左對齊

|3333.33  |

(

 負數括在括弧內

(3333.33)

 , 

添加分組分隔字元

3,333.33

# (對於 f ) 

包含小數點

 3,333.

# (for x or o) 

添加首碼 0x 或 0 

0xcafe

轉化為大寫

 0XCAFE

$  

指定格式化參數索引,如%1$d,%1$d表示以十進位和十六進位列印第一個參數

159 9F

格式化前面參數,如%d%<x表示以十進位和十六進位列印同一個參數

159 9F

時間日期的轉換符:

    轉換符  類型 舉例

  C 完整日期和時間 Mon Feb 09 18:05:19 PST 2004

  F ISO 8601 日期 2004-02-09

  D 美國時間格式 (mm/dd/year) 02/09/2004

  T 24小時時間 18:05:19

  r 12小時時間 06:05:19 pm

  R 24小時無秒時間 18:05

  Y 四位年 2004

  y 年的後兩位 04

  C 年的前兩位 20

  B 月的完整拼字 February

  b or h  月的縮寫 Feb

  m 兩位月(前補0) 02

  d 兩位日(前補0) 09

  e 日期(前不補0) 9

  A 完整星期幾 Monday

  a 星期幾的縮寫 Mon

  j 這一年的第多少天,三位補0 069

  H 24小時制小時,兩位補0 18

  k 24小時制小時,兩位不補0 18

  I 12小時制小時,兩位補0 06

  l 12小時制小時,兩位不補0 6

  M 分鐘,兩位補0 05

  S 秒,兩位補0 19

  L 毫秒,三位補0 047

  N 毫微秒,九位補0 047000000

  P 上下午大寫 PM

  p 上下午小寫 pm

  z RFC 822 numeric offset from GMT -0800

  Z 時區 PST

  s 1970-01-01 00:00:00起秒數 1078884319

  E 1970-01-01 00:00:00起毫秒數  1078884319047

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.