Java's input and output

  Export

  In the previous code, we always () to output something to the screen with System.out.println:

  println is an abbreviation of the print line, and line represents the output. Thus, if not want to change the output line, you can print ()

public class Main {
    public static void main(String[] args) {
        System.out.print("A,");
        System.out.print("B,");
        System.out.print("C.");
        System.out.println();
        System.out.println("END");
    }
}

   Export

A,B,C.
END

   Formatted output

  Java also provides the ability to format output. Why do you want to format the output? Because the computer representation of the data is not necessarily suitable for people to read it:

public class Main {
    public static void main(String[] args) {
        double d = 12900000;
        System.out.println(d); // 1.29E7
    }
}

   Export

1.29E7

   If the data is to be displayed as we desired format, it is necessary to use the function of the formatted output. Formatted output using System.out.printf (), by using the placeholder% ?, printf () can be formatted into the back of the parameters specified format:

{the Main class public 
    public static void main (String [] args) { 
        Double D = 3.1415926; 
        System.out.printf ( "%. 2F \ n-.", D); // show two decimal places 3.14 
        System.out.printf ( "% .4f \ n", d ); // display 4 decimal places 3.1416 
    } 
}

   Export

3.14
3.1416

   If you run error

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, double)
	The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, double)

	at Main.main(Main.java:4)

   Eclipse default in JAVA compatible version 1.4 instead 1.5 and above on the line. Project "Properties" Java complier "complier compliance lever: 1.5

 

 

   Java formatting There are more placeholders, can be formatted into a variety of data types specified string:

  

Placeholder Explanation
%d Formatted output integer
%x Formatted output hexadecimal integer
%f Formatted output float
%e Floating point numbers formatted output scientific notation
%s Format string

 

  

  

Guess you like

Origin www.cnblogs.com/minseo/p/11821103.html