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

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


  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.     public static 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.     } 
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:20	Oct:24	Hex:0x14
		System.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 2013
		System.out.printf("%1$s %2$tY %2$tB %2$td\n", "Now:",new Date());//output:2013 一月 16
		System.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值