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

How to input and output in Java

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

Share

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

Most people do not understand the knowledge points of this article "how to input and output in Java", so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to input and output in Java" article.

The first input method: scannerimport java.util.Scanner; / / Import java.util.Scannerpublic class User {public static void main (String [] args) {var sc = new Scanner (System.in); / / generate scanner object System.out.print ("Please enter your name:"); String name = sc.nextLine () / / enter the string System.out.print ("Please enter your age:"); int age = sc.nextInt (); / / enter the integer System.out.println ("name:" + name + "\ n" + "Age:" + age);}}

Running result

The commonly used next () method

NextInt () enter an integer

NextLine () input string

NextDouble () input double precision number

Next () input string with a space as a delimiter

The second input method: JOptionPane

Running result

The third input method: io.Console

This input method is a safe input method and can only be run in the cmd console, not in tools such as IDEA, eclipse, etc.

Create a User.java file

Import java.io.Console;// import java.io.Consolepublic class User {public static void main (String [] args) {Console con = System.console (); / / establish security input String name = con.readLine ("Please enter account:"); String passworld = con.readLine ("Please enter password:"); System.out.printf ("account:% s", name) System.out.printf ("% n password:% s", passworld);}}

Cmd goes to the directory where the file is located and runs the following instructions

Java-Dfile.encoding=UTF-8 User.java

Running result

Output method first output method: System.out.print ()

System.out.print () output directly without line wrapping

Public class User {public static void main (String [] args) {int a = 1; int b = 2; System.out.print (a); / / print () does not wrap the value of output a System.out.print (b); / / print () does not wrap the value of output b}}

Running result

twelve

The second output method: System.out.println ()

System.out.println () newline output

Public class User {public static void main (String [] args) {int a = 1; int b = 2; System.out.println (a); / / println () the value of the newline output a System.out.println (b); / / println () the value of the newline output b}}

Running result

one

two

The third output method: System.out.printf ()

System.out.printf () formatted output

Public class User {public static void main (String [] args) {int a = 1; int b = 2; System.out.printf ("a=%d%n", a); / / printf () formatted output System.out.printf ("baked% d", b); / / printf () formatted output}}

Running result

Axi1

Bread2

The above is about the content of this article on "how to input and output in Java". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

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