quine--Output Program source code program (Java)

Source: Internet
Author: User

Today see a very interesting question, how to output a program of their own source code, see the first response to this problem is naturally read the source file, and then output line by row.

However, if you add a restriction, you cannot read the file.

Emmm ... After thinking for a long time, the wiki rescued me, and the following was excerpted from the wiki. First on the source code.

 Public classquine{ Public Static voidMain (string[] args) {CharQ = 34;//quotation mark characterString[] L = {//Array of source code"Public class Quine",    "{",    "public static void Main (string[] args)",    "  {",    "Char q = 34; Quotation mark character ",    "string[] L = {//Array of source code",    "    ",    "    };",    "For" (int i = 0; I < 6; i++)//Print Opening code ",    "System.out.println (L[i]);",    "For" (int i = 0; i < l.length; i++)//Print string array ",    "System.out.println (L[6] + q + l[i] + q + ', ');",    "For" (int i = 7; i < l.length; i++)//Print This Code ",    "System.out.println (L[i]);",    "  }",    "}",    };  for(inti = 0; I < 6; i++)//Print Opening CodeSystem.out.println (L[i]);  for(inti = 0; i < l.length; i++)//Print string ArraySystem.out.println (L[6] + q + l[i] + q + ', ');  for(inti = 7; i < l.length; i++)//Print This codeSystem.out.println (L[i]); }}

The first half of the program is the Java run must prefix, and then add a variable Q (the content is quoted ""), the middle part of a string array to record the entire program source code (including the output source), the latter part is used to output the source code. Emm Anyway read this code, feel enlightened, this blog is not a technical blog, just to Quine do a record, if you see this blog on other languages how to achieve quine curious can see https://en.m.wikipedia.org/wiki/ Quine_ (computing), the source code is also on the wiki.

The following source code is on the basis of Quine, slightly modified a little bit of Java source code in the package also output implementation. (actually did not change what, main or Quine's realization is 2333).

  

 PackageXJBG; Public classquine{ Public Static voidMain (string[] args) {CharQ = 34;//quotation mark characterString[] L = {//Array of source code"Package XJBG;",    "",    "Public class Quine",    "{",    "public static void Main (string[] args)",    "  {",    "Char q = 34; Quotation mark character ",    "string[] L = {//Array of source code",    "    ",    "    };",    "For" (int i = 0; I < 8; i++)//Print Opening code ",    "System.out.println (L[i]);",    "For" (int i = 0; i < l.length; i++)//Print string array ",    "System.out.println (L[8] + q + l[i] + q + ', ');",    "For" (int i = 9; i < l.length; i++)//Print This Code ",    "System.out.println (L[i]);",    "  }",    "}",    };  for(inti = 0; I < 8; i++)//Print Opening CodeSystem.out.println (L[i]);  for(inti = 0; i < l.length; i++)//Print string ArraySystem.out.println (L[8] + q + l[i] + q + ', ');  for(inti = 9; i < l.length; i++)//Print This codeSystem.out.println (L[i]); }}

If there is an import can also be added to the beginning of the array, then this record blog to this end!

quine--Output Program source code program (Java)

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.