Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the input and output method of common data types in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces Java commonly used data type input and output methods are what, the text is very detailed, has a certain reference value, interested friends must read!

1. Char type

The char type mentioned here refers to the case where only one character is input.

1.1 Input format: import java.io.IOException;//import package public class Main { public static void main(String[] args) throws IOException { char ch = (char)System.in.read();// System.out.println((int)ch); }}

Note: It needs to be used with IOException exception. System.in is input from a standard input stream (most commonly a keyboard), and rand() reads the input from that stream. The input result of is int type and needs to be forcibly converted to char type.

1.2 exemplified

2, int type 1.1 simple int format input:

This refers to the case where there is only one int-formatted input per line. For example, enter only one integer per line.

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 Examples

Note: Long num is required, otherwise num is very large, the result is inaccurate.

2.1带空格的int格式输入 :

类似于 23 34这样的格式。两个数之间带了空格,这个时候,单用int输入是解决不了问题的,这个时候,需要换一种角度。我们将23 34整体看成一个字符串,然后在空格处分割,分成23和34两个字符串,在将这两个字符串转换成整型就可以了。关于split()的方法请查看官方的帮助手册。

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保留两位格式输出

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 举例说明

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语言中,循环多次输入有两种较为简单的方式:

while(scanf("%d", &n) != EOF) while(~scanf("%d", &n) )

在Java中,也有简单的方式:

while(scan.hasNext())1.2 举例说明

要注意的是多组输入单个字符的时候,需要以字符串格式输入来转换成字符类型。

5、数组

输入和C语言中的相似。不过,要注意的是字符串这样的输入,在C语言中,它是指针类型,在Java中,有它专属的字符串类型,不能像C语言那样,没有学指针之前,循环输入每一个字符组成字符串。

1.1 数组输入格式: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()方法即可。

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 字符串转换成整型,浮点型(以整型为例)int a = Integer.parseInt(str[0]);//假设分割后str[0]是一个字符'1'1.2 整型,浮点型转换成字符串int num = 10;// 方法1String str1 = num + "";//""表示空串,不同于Java中的null// 方法2String str2 = String.valueOf(num);2.1 字符串转换成字符数组 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 字符数组转换成字符串// 方法1new String(arr);//方法2String.copyValueOf(arr);3 举例说明

描述:写一个函数,输入一个字符串,实现字符串的逆置。代码如下:

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); }}

结果如下:

以上是"Java中常用数据类型的输入输出方法是什么"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report