Results 1 to 11 of 11
- 10-25-2011, 10:18 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
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.
- 10-25-2011, 10:32 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
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.
- 10-25-2011, 11:11 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
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.
- 10-26-2011, 11:13 AM #4
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
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
- 10-26-2011, 11:45 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
Re: New to java, challenging myself. But now I need help!
please delete
Last edited by instinctx; 10-26-2011 at 12:49 PM.
- 10-26-2011, 12:19 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
Re: New to java, challenging myself. But now I need help!
please delete
Last edited by instinctx; 10-26-2011 at 12:50 PM.
- 10-26-2011, 12:43 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
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]
- 10-26-2011, 12:44 PM #8
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
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.
- 10-26-2011, 01:33 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
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-26-2011, 02:05 PM #10
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
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)
- 10-26-2011, 02:42 PM #11
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
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
-
challenging error
By diwakardelhi in forum AWT / SwingReplies: 3Last Post: 11-15-2009, 09:43 PM -
Challenging error
By diwakardelhi in forum AWT / SwingReplies: 2Last Post: 11-15-2009, 09:40 PM -
non preemptive scheduling.....challenging!!
By snehil2009 in forum New To JavaReplies: 5Last Post: 11-10-2009, 09:23 AM -
challenging problems
By jayant3001 in forum JCreatorReplies: 24Last Post: 10-30-2008, 05:41 AM -
Is it possible to make this in Java? Challenging question.
By matt_well in forum New To JavaReplies: 24Last Post: 07-29-2008, 04:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks