Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default hi help me please

    i dont know how to put the user's input with this..when i run this it automatically runs without asking how many asterisk..im new in java :(
    Java Code:
    int x,y;
    for(x=5;x>=1;x--)
    {
      for(y=1;y<=1;y++)
        {System.out.print("*");}
      {System.out.println("");}
    }
    }
    }
    the output of this is

    *****
    ****
    ***
    **
    *

    i want the output is

    Enter number:
    (then it depends on the number in how many asterisk are there)
    Last edited by sunde887; 09-06-2011 at 04:54 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: hi help me please

    how to put the user's input with this
    See the Scanner class for methods that will read in user input.

    Search on this forum for code examples

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: hi help me please

    The code you posted does not produce the output you claim. The inner loop is totally pointless as it can only ever iterate once.

  4. #4
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Re: hi help me please

    im also new, however i know that it is easier to write a for loop like this

    Java Code:
    for(int i = 5; i >= 1; i--) {
        
    }
    you dont need to declare the int outside of the loop, this way its easier to read if you have lots of variables

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: hi help me please

    Quote Originally Posted by Bimz View Post
    you dont need to declare the int outside of the loop
    In this case they do as the variable needs to initialised with user input.
    Java Code:
    int var = get user input;
    for( ; var > 0; var--) {
    
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: hi help me please

    Actually you can even get user input inside the loop. Something I have used in while loops. No idea why I never thought to use it in a for loop.
    Java Code:
    for(int var = get user input; var > 0; var--) {
    
    }

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •