java输出35的值是_Java之输入输出

输入

在有多行数据输入的情况下,一般这样处理

static Scanner in = new Scanner(System.in);

while(in.hasNextInt())

或者是

while(in.hasNext())

格式1:Scanner sc = new Scanner (new BufferedInputStream(System.in));

格式2:Scanner sc = new Scanner (System.in);

在读入数据量大的情况下,格式1的速度会快些。

读一个整数: int n = sc.nextInt(); 相当于scanf("%d", &n);或 cin >> n;

读一个字符串:String s = sc.next(); 相当于 scanf("%s", s); 或 cin >> s;

读一个浮点数:double t = sc.nextDouble(); 相当于 scanf("%lf", &t);或cin >> t;

读一整行: String s = sc.nextLine();相当于 gets(s);或 cin.getline(...);

判断是否有下一个输入可以用sc.hasNext()或sc.hasNextInt()或sc.hasNextDouble()或sc.hasNextLine()

例题

例1:

输入数据有多组,每组占一行,由一个整数组成。

Sample Input

12

45

68

119

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc =new Scanner(System.in);

while(sc.hasNext()){ //判断是否结束

int score = sc.nextInt();//读入整数

}

}

}

例2:

输入数据有多组,每组占2行,第一行为一个整数N,指示第二行包含N个实数,每个实数按空格隔开。

Sample Input

2

12.5 67.2

5

12.9 90.7 90.5 64.8 34.9

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc =new Scanner(System.in);

while(sc.hasNext()){

int n = sc.nextInt();

for(int i=0;i

double a = sc.nextDouble();

}

}

}

}

例3:

输入数据有多组,第一行是一个整数N,表示包含N个字符串,后面跟着N行,每行包括一个由字母和数字组成的字符串。

Sample Input

2

asdfasdf123123asdfasdf

ASFfdgads12dfad34234

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

for (int i = 0; i < n; i++) {

String str = sc.next();

}

}

}

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = Integer.parseInt(sc.nextLine());

for (int i = 0; i < n; i++) {

String str = sc.nextLine();

}

}

}

例4:

给定一个日期,输出这个日期是该年的第几天。

输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成

Sample Input

1985/1/20

2006/3/12

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int[] dd = {0,31,28,31,30,31,30,31,31,30,31,30,31};

while(sc.hasNext()){

int days = 0;//初始化天数

String str = sc.nextLine();

String[] date = str.split("/");//根据“/”分割字符串为年月日

int y = Integer.parseInt(date[0]);

int m = Integer.parseInt(date[1]);

int d = Integer.parseInt(date[2]);

// 公历纪年法中,能被 4 整除的大多是闰年

// 但能被 100 整除 而不能被 400 整除的年份不是闰年

// 如 1900 年是平年,2000 年是闰年

//闰年共有366天(1-12月分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天)

if((y%400 == 0 || (y%4 == 0 && y%100 !=0)) && m>2) days ++;//当月份大于2时天数加1

days += d;

for(int i=1;i<=m;i++){

days += dd[i];

}

System.out.println(days);

}

}

}

输出

函数:

System.out.print(); /System.out.println();

System.out.format();

System.out.printf();

例1

public static void main(String[] args) {

int x = 5;

double y = 3.141592;

// 一般方式

System.out.println("x = " + x + ", y = " + y);

// printf()方式

System.out.printf("x = %d, y = %f\n", x, y);

// format()方式

System.out.format("x = %d, y = %f\n", x, y);

}

输出的结果如下:

x= 5, y = 3.141592

x = 5, y = 3.141592

x = 5, y = 3.141592

例2

public class Main {

public static void main(String[] args) {

double a = 0;

System.out.format("%.6f\n",a);

System.out.printf("%.6f",a);

}

}

输出结果如下:

0.000000

0.000000

Formatter类

在Java中,所有新的格式化功能都由Formatter类处理,上述的printf与format也是。可以将Formatter看作是一个翻译器,它将你的格式化字符串与数据翻译成需要的结果。当你创建一个Formatter对象的时候 ,需要向其构造器传递一些信息,告诉它最终的结果将向哪里输出

import java.util.Formatter;

public class FormatTest3 {

static Formatter formatter = new Formatter(System.out);

public static void printTitle() {

formatter.format("%-15s %-5s %-10s\n", "huhx", "linux", "liuli");

formatter.format("%-15s %-5s %-10s\n", "zhangkun", "yanzi", "zhangcong");

formatter.format("%-15s %-5s %-10s\n", "zhangkun", "yanzhou", "zhangcong");

}

public static void print() {

formatter.format("%-15s %5d %10.2f\n", "My name is huhx", 5, 4.2);

formatter.format("%-15.4s %5d %10.2f\n", "My name is huhx", 5, 4.1);

}

public static void main(String[] args) {

printTitle();

System.out.println("----------------------------");

print();

formatter.close();

}

}

输出结果如下:

huhx linux liuli

zhangkun yanzi zhangcong

zhangkun yanzhou zhangcong

----------------------------

My name is huhx 5 4.20

My n 5 4.10

规格化的输出

函数:

// 这里0指一位数字,#指除0以外的数字(如果是0,则不显示),四舍五入.

DecimalFormat fd = new DecimalFormat("#.00#");

DecimalFormat gd = new DecimalFormat("0.000");

System.out.println("x =" + fd.format(x));

System.out.println("x =" + gd.format(x));

import java.text.DecimalFormat;

import java.text.NumberFormat;

public class Main {

public static void main(String[] args) {

NumberFormat formatter = new DecimalFormat( "000000");

String s = formatter.format(-1234.567); // -001235

System.out.println(s);

formatter = new DecimalFormat( "##");

s = formatter.format(-1234.567); // -1235

System.out.println(s);

s = formatter.format(0); // 0

System.out.println(s);

formatter = new DecimalFormat( "##00");

s = formatter.format(0); // 00

System.out.println(s);

formatter = new DecimalFormat( ".00");

s = formatter.format(-.567); // -.57

System.out.println(s);

formatter = new DecimalFormat( "0.00");

s = formatter.format(-.567); // -0.57

System.out.println(s);

formatter = new DecimalFormat( "#.#");

s = formatter.format(-1234.567); // -1234.6

System.out.println(s);

formatter = new DecimalFormat( "#.######");

s = formatter.format(-1234.567); // -1234.567

System.out.println(s);

formatter = new DecimalFormat( ".######");

s = formatter.format(-1234.567); // -1234.567

System.out.println(s);

formatter = new DecimalFormat( "#.000000");

s = formatter.format(-1234.567); // -1234.567000

System.out.println(s);

formatter = new DecimalFormat( "#,###,###");

s = formatter.format(-1234.567); // -1,235

System.out.println(s);

s = formatter.format(-1234567.890); // -1,234,568

System.out.println(s);

// The ; symbol is used to specify an alternate pattern for negative values

formatter = new DecimalFormat( "#;(#) ");

s = formatter.format(-1234.567); // (1235)

System.out.println(s);

// The ' symbol is used to quote literal symbols

formatter = new DecimalFormat( " '# '# ");

s = formatter.format(-1234.567); // -#1235

System.out.println(s);

formatter = new DecimalFormat( " 'abc '# ");

s = formatter.format(-1234.567); // - abc 1235

System.out.println(s);

formatter = new DecimalFormat( "#.##%");

s = formatter.format(-12.5678987);

System.out.println(s);

}

}

字符串处理

String 类用来存储字符串,可以用charAt方法来取出其中某一字节,计数从0开始:

String a = "Hello"; // a.charAt(1) = 'e'

用substring方法可得到子串

System.out.println(a.substring(0, 4)) // output "Hell"

注意:第2个参数位置上的字符不包括进来。这样做使得 s.substring(a, b) 总是有 b-a个字符。

字符串连接可以直接用 + 号,如

String a = "Hello";

String b = "world";

System.out.println(a + ", " + b + "!"); // output "Hello, world!"

如想直接将字符串中的某字节改变,可以使用另外的StringBuffer类。

进制转换

// 把num当做10进制的数转成base进制的st(base <= 35).

String st = Integer.toString(num, base);

// 把st当做base进制,转成10进制的int(parseInt有两个参数,第一个为要转的字符串,第二个为说明是什么进制).

int num = Integer.parseInt(st, base);

// st是字符串,base是st的进制.

BigInter m = new BigInteger(st, base);

数组排序

函数:Arrays.sort();

import java.io.BufferedInputStream;

import java.util.Arrays;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner cin = new Scanner(new BufferedInputStream(System.in));

int n = cin.nextInt();

int a[] = new int[n];

for (int i = 0; i < n; i++) a[i] = cin.nextInt();

Arrays.sort(a);

for (int i = 0; i < n; i++) System.out.print(a[i] + " ");

}

}

输入

5

3 4 1 2 5

输出结果

1 2 3 4 5

高精度

BigInteger和BigDecimal可以说是acmer选择java的首要原因。

函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换为BigInteger(BigDecimal),用函数BigInteger.valueOf().

import java.io.BufferedInputStream;

import java.math.BigInteger;

import java.util.Scanner;

public class BigDecimalTest {

public static void test(String[] args) {

Scanner cin = new Scanner (new BufferedInputStream(System.in));

int a = 123, b = 456, c = 7890;

BigInteger x, y, z, ans;

x = BigInteger.valueOf(a);

y = BigInteger.valueOf(b);

z = BigInteger.valueOf(c);

ans = x.add(y); System.out.println(ans);

ans = z.divide(y); System.out.println(ans);

ans = x.mod(z); System.out.println(ans);

if (ans.compareTo(x) == 0) System.out.println("1");

}

}

有关System.nanoTime()函数的使用

该函数用来返回最准确的可用系统计时器的当前值,以毫微秒为单位。

long startTime = System.nanoTime();

*// ... the code being measured ...*

long estimatedTime = System.nanoTime() - startTime;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值