Algorithm (4th edition) -1.1.9 input and output

Source: Internet
Author: User

Summary: This section describes the input and output knowledge, including standard output, formatted output, standard input, redirection and piping, file-based input and output, standard drawing library, and so on.

Focus:

1. In our model, Java programs can get input from command-line arguments or an abstract character stream called the standard input stream, and write the output to another string called the standard output stream.

2. Format the output rule:

· Common conversion codes include the following:

· D: decimal number for Java integral type

· F: Floating point number

· S: string

· You can insert an integer between the% and the conversion code to represent the width of the converted value, that is, the length of the output string.

By default, the conversion will add a space to the left of the string to reach the desired width, and if we want to add a space to the right, it should be a negative width (if the converted string is longer than the set width, the width is ignored).

· After the width we can also insert a decimal point and a numeric value to specify the number of decimal places (precision) to retain for the converted double values, or the length of the string to intercept.

3. By combining these, redirecting the output of one program to the input of another program is called a pipe.

This difference is far-reaching because he breaks the limit of the length of the input and output stream we can handle.

4. In this book, we will use Stddraw in the visualization of data analysis and algorithms.

5.StdDraw Drawing Examples:

· function value

 public  class   Hanshuzhi { static  void   main (string[] args) {int  N = 100;        Stddraw.setxscale ( 0 0, N * n); Stddraw.setpenradius (.         01);  for  (int  i = 1; I <= N; i++) {Stddraw.point (I, I);            Stddraw.point (i, I  * i);        Stddraw.point (i, I  * Math.log (i)); }    }}

· Random array

@SuppressWarnings ("Deprecation") Public classSuijishuzu { Public Static voidMain (string[] args) {intN = 50; Double[] A =New Double[N];  for(inti = 0; i < N; i++) A[i]=Stdrandom.random ();  for(inti = 0; i < N; i++) {            Doublex = 1.0 * I/N; Doubley = A[i]/2.0; DoubleRW = 0.5/N; DoubleRH = A[i]/2.0;        Stddraw.filledrectangle (x, y, RW, RH); }    }}

· Sorted random Array

ImportJava.util.*; @SuppressWarnings ("Deprecation") Public classYipaixudesuijishuzu { Public Static voidMain (string[] args) {intN = 50; Double[] A =New Double[N];  for(inti = 0; i < N; i++) A[i]=Stdrandom.random ();        Arrays.sort (a);  for(inti = 0; i < N; i++) {            Doublex = 1.0 * I/N; Doubley = A[i]/2.0; DoubleRW = 0.5/N; DoubleRH = A[i]/2.0;        Stddraw.filledrectangle (x, y, RW, RH); }    }}

Algorithm (4th edition) -1.1.9 input and output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.