Core java 输入和输出实例(很有用,让我明白了很多,谢谢作者)

来源:互联网 发布:以图搜图的软件 编辑:程序博客网 时间:2024/04/29 23:17

一个简单的java输入输出实例,包含了最常用的输入输出函数。


[java] view plaincopyprint?
  1. package com.xujin; 
  2.  
  3. import java.util.Date; 
  4. import java.util.Scanner; 
  5.  
  6. public class MyTest{ 
  7.     static Scanner cin =new Scanner(System.in); 
  8.     publicstatic void main(String[] args){  
  9.          
  10.         System.out.print("input your name:"); 
  11.         String name = cin.next();//读入下一个单词,以空格作为标准 
  12.         System.out.print("input your age:"); 
  13.         int age = cin.nextInt();//读入下一个int,以空格作为标准 
  14.         System.out.print("input your salary:"); 
  15.         double salary = cin.nextDouble(); 
  16.         System.out.print("input detail:"); 
  17.         String detail = cin.nextLine();//读入下一行,这里是读入上一次的回车 
  18.         detail = cin.nextLine();//读入下一行 
  19.          
  20.         //格式化输出 
  21.         System.out.printf("Name:%s\n", name); 
  22.         System.out.printf("Age:%d\tOct:%<o\tHex:%1$#x\n",age);//output:Age:20    Oct:24  Hex:0x14 
  23.         System.out.printf("Salary:%#,8.2f\nDetail:",salary); 
  24.         System.out.println(detail); 
  25.          
  26.         //时间输出 
  27.         System.out.printf("Now:%tc\n",new Date());//output:星期三 一月 16 21:02:10 CST 2013 
  28.         System.out.printf("%1$s %2$tY %2$tB %2$td\n","Now:",new Date());//output:2013 一月 16 
  29.         System.out.printf("%s %tY %<tB %<td\n","Now:",new Date());//output:2013 一月 16 
  30.          
  31.         //使用静态的String.format方法常见一个格式化的字符串,而不打印输出 
  32.         System.out.println(detail); 
  33.         String message = String.format("Hello,%s! You will be %d next year", name, ++age); 
  34.         System.out.print(message); 
  35.     } 


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

原创粉丝点击