Results 1 to 10 of 10
  1. #1
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Need help getting two loopslined up beside eachother

    Hey guys i need help getting these two loops line up beside each other truculently it goes hours123456pay481216 and so on but vertical i need them to line up beside each other, hints or help is welcome and much appreciated.
    Java Code:
    public class Hourlypay
    {
    	public static void main(String args[])
    	{
    		
    		System.out.println("Hours" );
    		
    		for (int i = 1; i <=40; i++)
    		{
    			System.out.println(i + " ");
    		}
    		System.out.println("Pay");
    		
    		for (int p = 4; p <= 160; p = p + 4)
    			
    				
    		{
    			
    			System.out.println(p + " ");
    		}
    		
    		
    		
    	}
    }

  2. #2
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: Need help getting two loopslined up beside eachother

    I've not the slightest idea what you are asking. Perhaps you want each print statement to be on a new line? If so, using this character inside of a string will create a new line: \n

    You should try to better explain what you want next time, perhaps show an example.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Need help getting two loopslined up beside eachother

    See the documentation for String#format(...) / java.util.Formatter and PrintStream#printf(...) [System.out is a PrintStream]

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Need help getting two loopslined up beside eachother

    If you want:
    Java Code:
    Hours    Pay
    1        4
    2        8
    3        12
    Then you need to do that in a single loop.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Need help getting two loopslined up beside eachother

    Quote Originally Posted by Tolls View Post
    If you want:
    Java Code:
    Hours    Pay
    1        4
    2        8
    3        12
    Then you need to do that in a single loop.
    yes that is exactly what i mean but how do i do that in a singe loop? i cant figure out how to have two variables working at the same time.

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

    Default Re: Need help getting two loopslined up beside eachother

    how to have two variables working at the same time.
    You can define additional variables outside of the one used to control the for loop and use them inside the loop.

    define a var and give it a value
    begin loop
    use var here: increment and print as needed
    end loop
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Need help getting two loopslined up beside eachother

    Quote Originally Posted by Norm View Post
    You can define additional variables outside of the one used to control the for loop and use them inside the loop.

    define a var and give it a value
    begin loop
    use var here: increment and print as needed
    end loop
    Hey man, im not quite sure what you mean, could you explain it a little better please.

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

    Default Re: Need help getting two loopslined up beside eachother

    Here it is in code:
    Java Code:
    int x = 4;  // define a variable and give it a value
    for(int i=0; i < 3; i++) {   // begin a loop
      x++; // increment x
      System.out.println("x="+x);  // print x
    } //end loop
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: Need help getting two loopslined up beside eachother

    Ahh i see thanks, but now i an curious, this isnt really a question but i want to know if its possible. i need to square root a number could i do it like this or would i have to do it another way? as you can see below i need the number that is being looped to show but i need to square it every time it changes.
    Java Code:
    		for(int a = 1; a <= 40; a++) 
    			int b = a;
    			Math.sqrt(b);
    {
    			System.out.println(a + " " + b);
    		}
    Last edited by MrPosotive; 10-05-2012 at 05:09 AM.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Need help getting two loopslined up beside eachother

    If a methods returns a value, you need to assign that value to a variable of an appropriate type. This line of your code
    Java Code:
    Math.sqrt(b);
    discards the value returned by sqrt(...).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Battleship Game - Pieces lay on top of eachother.
    By lolerskates2695 in forum New To Java
    Replies: 7
    Last Post: 04-25-2011, 01:05 AM
  2. How to make two jlists import objects from eachother
    By Embercloud in forum New To Java
    Replies: 0
    Last Post: 12-30-2010, 05:24 PM

Posting Permissions

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