Results 1 to 6 of 6
  1. #1
    Hello is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Parameters with two integers.

    Hello there, im stuck at something here. I'm new to Java, and i have this issue. I'll try to explain as good as i can.

    I have written a method called printNumbers, in this method it has a paramater including two integers ( public static int printNumbers (int x, int y) ), i want it to print all the numbers from 1 up to (and including) the value of the first parameter. The second parameter detemines how many numbers to be printed on each line. And in the main method i use scanner as user input. Could anyone help me out here?
    Here is an "psudo" picture i made to illustrate the problem.
    Parameters with two integers.-psudojavaill.jpg
    Last edited by Hello; 10-25-2011 at 03:16 PM.

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

    Default Re: Parameters with two integers.

    If I understand your problem correctly...

    int total = 0;

    for(int i=1;i<x+1;i++)
    {
    total+=x;
    }

    You are probably already using a loop to print so just add this in there. Remember that you are starting at 1, not 0.

  3. #3
    Hello is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: Parameters with two integers.

    Hello joey, thanks for reply. I finaly got it to work. Here you can see how i solved this.
    Java Code:
    import java.util.Scanner;
    
    public class Boka {
    
    	public static void main (String args[]){
    		
    		Scanner input = new Scanner(System.in);
    		
    		int number, line;
    		
    		System.out.print("Max number: ");
    		number = input.nextInt();
    		
    		System.out.print("Numbers each line: ");
    		line = input.nextInt();
    		
    		System.out.print( printNumbers(number, line) );
    		
    		
    	}
    
    	public static int printNumbers(int x, int y){
    		
    		
    		
    		for (int i = 1; i < x; i++){
    			
    			
    			if (i % y == 0){
    				System.out.printf("%3s \n", i );
    			
    			}
    			else {
    				System.out.printf("%3s", i );
    			}
    		}
    		System.out.print(" ");
    		return x;
    		 
    		
    		
    	
    		
    	}
    	
    }
    Is there a better way / less code etc.?

    Kind Regards Danny

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

    Default Re: Parameters with two integers.

    There is ALWAYS a leaner way. Check out ternary operators if you want to have some fun. For readability, though, yours is fine.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default Re: Parameters with two integers.

    Change line #26 to:

    Java Code:
        for (int i = 1; i <= x; i++){
    Your version forgot that last number (and you don't have to print it in your main( ... ) method as an afterthought)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Parameters with two integers.

    Interestingly, I think he got it by accident. He is returning x for some reason, which is being printed out in main.

Similar Threads

  1. how do you add up integers in an array?
    By shazakala in forum New To Java
    Replies: 7
    Last Post: 04-19-2011, 10:32 AM
  2. Replies: 5
    Last Post: 11-21-2010, 02:08 PM
  3. Set of Integers
    By rsjava24 in forum New To Java
    Replies: 7
    Last Post: 01-28-2010, 10:29 AM
  4. Integers and Lists
    By TGH in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 09:49 AM
  5. Random Integers
    By www.kwalski.com in forum Java Applets
    Replies: 8
    Last Post: 12-09-2007, 05:49 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
  •