Java中常用数据类型的输入输出详解
本文主要介绍了Java中几个常用的数据类型是如何输入和输出的,例如:Char型、int型、double型、数组、字符串等,对我们学习java有一定的帮助,感兴趣的小伙伴可以跟随小编一起学习学习
目录
1、Char型
1.1 输入格式:
1.2 举例说明
2、int型
1.1 简单的int格式输入:
1.2 举例说明
2.1带空格的int格式输入 :
2.2 举例说明
3.1 复杂int格式的输入
3.2 举例说明
3、double型
4、多次输入
1.1 输入格式
1.2 举例说明
5、数组
1.1 数组输入格式:
2.1 数组转换成字符串
6、字符串
1.1 字符串转换成整型,浮点型(以整型为例)
1.2 整型,浮点型转换成字符串
2.1 字符串转换成字符数组
2.2 字符数组转换成字符串
3 举例说明
7、写在最后
C语言学习完转到Java后,第一个感觉就是Java的写法很复杂,同时,在常用的数据类型的输入输出上也没有C语言那么方便。在C语言上面,用 scanf 函数可以很方便的输入大多数格式,在Java上却不行,Java当中没有类似于scanf的语句。本文搭配本人的输入输出习惯及做题的记录,对这些不同类型的如整型的,是整型的但隔开参数的...做一个总结。下面的说明中主类均为Main类,我们使用Scanner来进行输入。每一种输入或输出可能有多种方法,本人只写了较为简单的写法。
1、Char型
这里说的char型指的是只输入一个字符的情况。
1.1 输入格式:
1 2 3 4 5 6 7 | import java.io.IOException; //导入包 public class Main { public static void main(String[] args) throws IOException { char ch = ( char )System.in.read(); //<1> System.out.println(( int )ch); } } |
说明:需要搭配IOException异常来使用。<1>中,System.in就是从标准输入流(最常见的是键盘)输入,rand()方法是从这个流里面读取输入的内容。<1>的输入结果是int型的,需要强制转换成char型的。
1.2 举例说明
2、int型
1.1 简单的int格式输入:
这里指的是每一行只有一个int型格式的输入的情况。比如,每一行只输入一个整型的情况。
1 2 3 4 5 6 7 8 9 10 | import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int num = scan.nextInt(); System.out.println(num); } } |
1.2 举例说明
备注:需要为Long num,否者num很大时,结果不准确。
2.1带空格的int格式输入 :
类似于 23 34这样的格式。两个数之间带了空格,这个时候,单用int输入是解决不了问题的,这个时候,需要换一种角度。我们将23 34整体看成一个字符串,然后在空格处分割,分成23和34两个字符串,在将这两个字符串转换成整型就可以了。关于split()的方法请查看官方的帮助手册。
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String[] str = scan.nextLine().split( "[ ]" ); //分成几块,就有几个字符串数组,这里是两块 int a = Integer.parseInt(str[ 0 ]); int b = Integer.parseInt(str[ 1 ]); //等等等... System.out.println(a + " " + b); } } |
2.2 举例说明
3.1 复杂int格式的输入
类似于输入a=3,b=2这样的,方法和2.1中说明的情况是一样的。这里我们直接上例题。
3.2 举例说明
long类型和int类型的输入类似,不再累述。
3、double型
在Java中,应该较多的使用double型,而不是float型。
浮点型的主要是它的格式化输出,比如保留两位小数这样的格式。在Java中,有类似于C语言那样的printf方法,我们还可以使用String中的format()方法来实现。
1.1 double保留两位格式输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); double num = scan.nextDouble(); String a = String.format( "%.2f" , num); System.out.println(a); } } //printf格式输出: //System.out.printf("%2f", num); |
1.2 举例说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str = scan.nextLine(); String[] num = str.split( "[;,]" ); String a = String.format( "%.2f" , Double.parseDouble((num[ 1 ]))); String b = String.format( "%.2f" , Double.parseDouble((num[ 2 ]))); String c = String.format( "%.2f" , Double.parseDouble((num[ 3 ]))); System.out.println( "The each subject score of No. " + num[ 0 ] + " is " + a + ", " + b + ", " + c + "." ); } } |
4、多次输入
1.1 输入格式
在C语言中,循环多次输入有两种较为简单的方式:
1 | while ( scanf ( "%d" , &n) != EOF) |
1 | while (~ scanf ( "%d" , &n) ) |
在Java中,也有简单的方式:
1 | while (scan.hasNext()) |
1.2 举例说明
要注意的是多组输入单个字符的时候,需要以字符串格式输入来转换成字符类型。
5、数组
输入和C语言中的相似。不过,要注意的是字符串这样的输入,在C语言中,它是指针类型,在Java中,有它专属的字符串类型,不能像C语言那样,没有学指针之前,循环输入每一个字符组成字符串。
1.1 数组输入格式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int [] arr = new int [ 3 ]; //输入3个 for ( int i = 0 ; i < arr.length; i++) { arr[i] = scan.nextInt(); } for ( int i = 0 ; i < arr.length; i++) { System.out.print(arr[i] + " " ); } } } |
2.1 数组转换成字符串
使用Arrays中的toString()方法即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int [] arr = new int [ 3 ]; //输入3个 for ( int i = 0 ; i < arr.length; i++) { arr[i] = scan.nextInt(); } System.out.println(Arrays.toString(arr)); } } |
输入1,2,3 结果你将会看见[1,2,3]。有时候OJ题的格式是1 2 3。[1,2,3]这样的格式也能通过。
6、字符串
由于大多数输入转为了字符串类型。因此,对于字符串,需要进行转换的操作比较多,比如分割后的字符串转换成整型,浮点型,数组等。
1.1 字符串转换成整型,浮点型(以整型为例)
1 | int a = Integer.parseInt(str[ 0 ]); //假设分割后str[0]是一个字符‘1' |
1.2 整型,浮点型转换成字符串
1 2 3 4 5 | int num = 10 ; // 方法1 String str1 = num + "" ; //“”表示空串,不同于Java中的null // 方法2 String str2 = String.valueOf(num); |
2.1 字符串转换成字符数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str = scan.nextLine(); char [] arr = str.toCharArray(); for ( int i = 0 ; i < arr.length; i++) { System.out.print(arr[i] + " " ); } } } |
2.2 字符数组转换成字符串
1 2 3 4 | // 方法1 new String(arr); //方法2 String.copyValueOf(arr); |
3 举例说明
描述:写一个函数,输入一个字符串,实现字符串的逆置。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.Scanner; public class Main { public static String my_reverse(String str) { int left = 0 ; int right = str.length() - 1 ; char [] arr = str.toCharArray(); while (left < right) { char tmp = 0 ; tmp = arr[left]; arr[left] = arr[right]; arr[right] = tmp; left++; right--; } return new String(arr); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str = scan.next(); String ret = my_reverse(str); System.out.println(ret); } } |
结果如下:
7、写在最后
这些输入是常见的。多写写代码就会熟练的掌握他们。
到此这篇关于Java中常用数据类型的输入输出详解的文章就介绍到这了
原文链接:https://blog.csdn.net/Naion/article/details/122020669
伪原创工具 SEO网站优化 https://www.237it.com/