Forum Stats

  • 3,758,936 Users
  • 2,251,478 Discussions
  • 7,870,439 Comments

Discussions

Can some one help me convert python code to java

Gayathri Venugopal
Gayathri Venugopal Member Posts: 108
edited Aug 30, 2018 8:02AM in Java User Groups Discussions

Basically, this program takes the minimum, maximum and interval of all the stars and prints the asterisks accordingly. I have written the python code as:

  1. <span class="kwd" style="color: #000088; margin-top: 0 !important;">print</span><span class="pun" style="color: #666600;">(</span><span class="str" style="color: #008800;">"This program prints out lines of stars\n\n"</span><span class="pun" style="color: #666600;">)</span>
  2. <span class="pln" style="color: #000000; margin-top: 0 !important;">minimum</span><span class="pun" style="color: #666600;">=</span><span class="kwd" style="color: #000088;">int</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: #000000;">input</span><span class="pun" style="color: #666600;">(</span><span class="str" style="color: #008800;">"What is the minimum number of stars?"</span><span class="pun" style="color: #666600;">))</span>
  3. <span class="pln" style="color: #000000; margin-top: 0 !important;">maximum</span><span class="pun" style="color: #666600;">=</span><span class="kwd" style="color: #000088;">int</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: #000000;">input</span><span class="pun" style="color: #666600;">(</span><span class="str" style="color: #008800;">"What is the maximum number of stars?"</span><span class="pun" style="color: #666600;">))</span>
  4. <span class="pln" style="color: #000000; margin-top: 0 !important;">interval</span><span class="pun" style="color: #666600;">=</span><span class="kwd" style="color: #000088;">int</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: #000000;">input</span><span class="pun" style="color: #666600;">(</span><span class="str" style="color: #008800;">"What is the interval?"</span><span class="pun" style="color: #666600;">))</span>
  5. <span class="kwd" style="color: #000088; margin-top: 0 !important;">print</span><span class="pun" style="color: #666600;">(</span><span class="str" style="color: #008800;">"\n"</span><span class="pun" style="color: #666600;">)</span>
  6. <span class="kwd" style="color: #000088; margin-top: 0 !important;">for</span><span class="pln" style="color: #000000;"> i </span><span class="kwd" style="color: #000088;">in</span><span class="pln" style="color: #000000;"> range</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: #000000;">minimum</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: #000000;">maximum</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: #000000;">interval</span><span class="pun" style="color: #666600;">):</span>
  7. <span class="pln" style="color: #000000; margin-top: 0 !important;">   </span><span class="kwd" style="color: #000088;">for</span><span class="pln" style="color: #000000;"> j </span><span class="kwd" style="color: #000088;">in</span><span class="pln" style="color: #000000;"> range</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: #000000;">i</span><span class="pun" style="color: #666600;">):</span>
  8. <span class="pln" style="color: #000000; margin-top: 0 !important;">   </span><span class="kwd" style="color: #000088;">print</span><span class="pun" style="color: #666600;">(</span><span class="str" style="color: #008800;">"*"</span><span class="pun" style="color: #666600;">,</span><span class="kwd" style="color: #000088;">end</span><span class="pun" style="color: #666600;">=</span><span class="str" style="color: #008800;">''</span><span class="pun" style="color: #666600;">)</span>
  9. <span class="kwd" style="color: #000088; margin-top: 0 !important;">print</span><span class="pun" style="color: #666600;">(</span><span class="str" style="color: #008800;">" (%d stars)"</span><span class="pun" style="color: #666600;">%</span><span class="pln" style="color: #000000;">i</span><span class="pun" style="color: #666600;">)</span>

Sample Run-

<span class="typ" style="color: #660066; margin-top: 0 !important;">What</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #000088;">is</span><span class="pln" style="color: #000000;"> the minimum number of stars</span><span class="pun" style="color: #666600;">?</span><span class="pln" style="color: #000000;"> </span><span class="lit" style="color: #006666;">3</span><span class="typ" style="color: #660066; margin-top: 0 !important;">What</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #000088;">is</span><span class="pln" style="color: #000000;"> the maximum number of stars</span><span class="pun" style="color: #666600;">?</span><span class="pln" style="color: #000000;"> </span><span class="lit" style="color: #006666;">15</span><span class="typ" style="color: #660066; margin-top: 0 !important;">What</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #000088;">is</span><span class="pln" style="color: #000000;"> the interval</span><span class="pun" style="color: #666600;">?</span><span class="pln" style="color: #000000;"> </span><span class="lit" style="color: #006666;">4</span><span class="pun" style="color: #666600; margin-top: 0 !important;">***</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #666600;">(</span><span class="lit" style="color: #006666;">3</span><span class="pln" style="color: #000000;"> stars</span><span class="pun" style="color: #666600;">)</span><span class="pun" style="color: #666600; margin-top: 0 !important;">*******</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #666600;">(</span><span class="lit" style="color: #006666;">7</span><span class="pln" style="color: #000000;"> stars</span><span class="pun" style="color: #666600;">)</span><span class="pun" style="color: #666600; margin-top: 0 !important;">***********</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #666600;">(</span><span class="lit" style="color: #006666;">11</span><span class="pln" style="color: #000000;"> stars</span><span class="pun" style="color: #666600;">)</span><span class="pun" style="color: #666600; margin-top: 0 !important;">***************</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #666600;">(</span><span class="lit" style="color: #006666;">15</span><span class="pln" style="color: #000000;"> stars</span><span class="pun" style="color: #666600;">)</span><span class="typ" style="color: #660066; margin-top: 0 !important;">This</span><span class="pln" style="color: #000000;"> program prints outlines of stars</span><span class="pun" style="color: #666600;">.</span>
Tagged:

Answers