Results 1 to 11 of 11
  1. #1
    instinctx is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default New to java, challenging myself. But now I need help!

    Hey, so I'm reading up on java. And started with printf, for, if, else, int/double etc.
    I got to write this code just perfectly (set in a number first, then say how many numbers you want on each line. And it will show you every number from 1 - the number set)
    Example:
    Set highest number: 90
    Set how many numbers for each line: 13
    1 2 3 4 5 6 7 8 9 10 11 12 13
    14 15 16 17 18 19 20 21 22 23 24 25 26
    27 28 29 30 31 32 33 34 35 36 37 38 39
    40 41 42 43 44 45 46 47 48 49 50 51 52
    53 54 55 56 57 58 59 60 61 62 63 64 65
    66 67 68 69 70 71 72 73 74 75 76 77 78
    79 80 81 82 83 84 85 86 87 88 89 90

    Process completed.

    -----

    But now I want something else. So I got a university school project that a friend gave to me. It was similar to the other task I got (the one above). But I was now going to get something like this. And I don't have a clue to get the numbers increasing range between them.
    This is the task (task is to get something like this. Btw, can't get spacebars between every number so I'm using "." please read the "." as one simple spacebar) looks like the spacebard (the dot) is increased by one for each line... hmmm:
    Set highest number : 55 /* just an example */

    1
    .2.3
    ..4..5..6
    ...7...8...9...10
    ...11...12...13...14...15
    ....16....17....18....19....20....21
    .....22.....23.....24.....25.....26.....27.....28
    ......29......30......31......32......33......34.. ....35......36
    .......37.......38.......39.......40.......41..... ..42.......43.......44.......45
    .........46.........47.........48.........49...... ...50.........51.........52.........53.........54. ........55


    Any tips?
    Last edited by instinctx; 10-25-2011 at 10:26 PM.

  2. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: New to java, challenging myself. But now I need help!

    It would be easier for us to help you if you showed us what you have so far in terms of code.

    That being said, the thing to do would have a variable to keep track of the number of periods needed. Increment this variable with each new line.

  3. #3
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: New to java, challenging myself. But now I need help!

    It looks like each line will have an ever increasing number of spaces. It also looks like you know how many lines there will be. SOunds like a for-loop. Get some code up here and we'll guide you along.

  4. #4
    instinctx is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: New to java, challenging myself. But now I need help!

    Yeah, will log on my "buisness PC" and link you what I've done so far, 2sec

  5. #5
    instinctx is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: New to java, challenging myself. But now I need help!

    please delete
    Last edited by instinctx; 10-26-2011 at 12:49 PM.

  6. #6
    instinctx is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: New to java, challenging myself. But now I need help!

    please delete
    Last edited by instinctx; 10-26-2011 at 12:50 PM.

  7. #7
    instinctx is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: New to java, challenging myself. But now I need help!

    So far (UPDATE!)

    Java Code:
    import java.util.Scanner;
    
    public class oppgave3c_simon_tysland {
    	public static void main(String[] args) {
    		
    	printNumbersIncreasing();
    	}
    	
    	public static void printNumbersIncreasing() {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Oppgi Ývre grense: ");
    		int ovreGrense = input.nextInt();
    		
    		
     for (int lineNumber = 1; lineNumber <= ovreGrense; lineNumber++) {
     for (int currentField = 1; currentField <= lineNumber; currentField++) {
     System.out.printf ("%" + lineNumber + "d", currentField);
       }
      			System.out.println();	} 
    	}
    	}
    Last edited by sunde887; 10-26-2011 at 03:29 PM. Reason: Changed quote tags to code tags...[code]...[/code]

  8. #8
    instinctx is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: New to java, challenging myself. But now I need help!

    current Output:

    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5

    Process completed.

  9. #9
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: New to java, challenging myself. But now I need help!

    I'm a bit confused. Did you want the format like
    Java Code:
    1
    1 2
    1  2  3
    1   2   3   4   5
    or
    1
    2 3
    4  5  6
    7   8   9

  10. #10
    instinctx is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: New to java, challenging myself. But now I need help!

    I want it to become like the last example:
    1
    2 3
    4 5 6
    7 8 9 10

    (I got the spacefunction worked out. Now I need the numbers to not reset itself for each line)

  11. #11
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: New to java, challenging myself. But now I need help!

    So right now, you have a nested loop that prints 1...n on each n line. Your structure should be a nested loop that prints n spaces between each number. So loop 1 will count 1...x (input variable). Loop 2 will count 1...n (line number), and print x. And Loop 3 will print spaces. Does that make sense?

Similar Threads

  1. challenging error
    By diwakardelhi in forum AWT / Swing
    Replies: 3
    Last Post: 11-15-2009, 09:43 PM
  2. Challenging error
    By diwakardelhi in forum AWT / Swing
    Replies: 2
    Last Post: 11-15-2009, 09:40 PM
  3. non preemptive scheduling.....challenging!!
    By snehil2009 in forum New To Java
    Replies: 5
    Last Post: 11-10-2009, 09:23 AM
  4. challenging problems
    By jayant3001 in forum JCreator
    Replies: 24
    Last Post: 10-30-2008, 05:41 AM
  5. Replies: 24
    Last Post: 07-29-2008, 04:04 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
  •