Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Beginner: For loop excercise (stuck) Posted by ObedMarsh on 8 Sept 2012 at 2:20 AM
Hi everyone

I recently started on my new education here in Denmark which focuses on system development and obviously a large part of that is programming. I'm going through some of the initial programming excercises and were doing for loops atm.

We are using the book "Building Java Programs - A back to basics approach (2nd edition)" and im doing an excercise that goes like this:

4. Write a method called printSquare that accepts a minimum and maximum integer and prints a square of lines of
increasing numbers. The first line should start with the minimum, and each line that follows should start with the
next-higher number. The sequence of numbers on a line wraps back to the minimum after it hits the maximum. For
example, the call printSquare(3, 7);

should produce the following output:

34567
45673
56734
67345
73456

If the maximum passed is less than the minimum, the method produces no output.


Currently my code is looking like this, but I can't seem to get the last part right:

public class Opg4Kap3 {

	public static void main(String[] args) {
	
	printSquare(3, 7);
	
	}
	
	public static void printSquare(int a, int b) {
	
		for (int k=a ; k<=b ; k++) {
			System.out.print(k);
				
				for (int i=k+1 ; i<=b ; i++) {
					System.out.print(i);
					
				}
				
				for (int r=a ; r<=6 ; r++) {
					System.out.print(r);
				}
					
				System.out.println();	
		} 
	
	
	}

}


If you compile it and run it you can see I'm getting this output, which is close but no cigar:

345673456
45673456
5673456
673456
73456

We don't need to turn this in or anything so I'm not really interested in the final answer, was just hoping someone could give me a clue to the next step so I can finish it though, since it annoys me I can't get the last part right (or if I have to do something completely different to begin with?).

Also this is not supposed to be done with anything but a method and for loops so no if statement tips or anything.

Thanks in advance



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.