How to realize input and output by java
Editor to share with you how to achieve java input and output, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
First, output to the console
Basic grammar
Public static void main (String [] args) {System.out.println ("output and wrap"); System.out.print ("output without wrapping"); / / the difference between print and println is whether printf in c language adds the difference System.out.printf ("% d\ n", 10); / / similar to printf in C language}
Let's take a look at the running results:
The content of 1.println output comes with\ nNo.\ n
The formatted output of 2.printf is basically the same as that of printf in C language.
Type 1 from the keyboard. Read a character (understand)
The code is as follows (example):
Public static void main (String [] args) throws IOException {/ / rare method ("enter a char:") System.out.println ("enter a char:"); char I = (char) System.in.read (); / / take reading char types as an example, you can also read other types, and you can System.out.println ("your char is:" + I). } 2.Scannerpublic static void main (String [] args) {Scanner scanner=new Scanner (System.in); / / you need import java.util.Scanner; before using Scanner, just like you need # include before using printf in C language / / the parameter System.in indicates that you enter int n=scanner.nextInt (); System.out.println (n) from the keyboard. / / here you use int n to receive data from scanner / / if you need other types of reception, such as long b, the code is changed as follows: long b=scanner.nextLong (); / / other types and so on, next whatever type of data you want to read in, System.out.println (b); / / about reading the string String str= scanner.nextLine () / / this is slightly different from the above, and it's easy to remember that reading a string of characters is System.out.println (str); / / but it's important to note that the runtime directly "didn't give me a chance to enter a string." why? / / explanation: here is the read string, you printed b in the last time, press an enter, the computer will think you want to receive an enter, so it has been received, will not give you a chance to enter / so! It is highly recommended that you put the input string in front of other types, otherwise it is very error prone to scanner.close (); / / scanner in java is something similar to a file. If you use this scanner, your related files are open, and you can close them. / / of course, it doesn't matter if you don't close it, and the program will be closed automatically at the end of the program.
Ps: some considerations about string input
Public static void main (String [] args) {Scanner scanner=new Scanner (System.in); String str=scanner.next (); / / here we enter " hello" System.out.println (str); / / only print / / explain: the difference between scanner.next () and scanner.nextLine () is that / / the former stops when reading a space, while the latter can read the space and the following string}.
3. Loop read public static void main (String [] args) {Scanner scanner=new Scanner (System.in); end reading {/ / end by ctrl+d in while (scanner.hasNextInt ()) / / idea. Here, take reading integers as an example. If you want to cycle through other data types, while (scanner.hasNext type name ()) can int n=scanner.nextInt (); System.out.println (n) }}
The running effect is as follows
The above is all the contents of the article "how to achieve input and output in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!