Results 1 to 6 of 6
Thread: Parameters with two integers.
 10252011, 02:53 PM #1Member
 Join Date
 Oct 2011
 Posts
 4
 Rep Power
 0
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.
Last edited by Hello; 10252011 at 03:16 PM.
 10252011, 05:49 PM #2Member
 Join Date
 Oct 2011
 Posts
 90
 Rep Power
 0
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.
 10252011, 08:24 PM #3Member
 Join Date
 Oct 2011
 Posts
 4
 Rep Power
 0
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; } }
Kind Regards Danny
 10252011, 08:31 PM #4Member
 Join Date
 Oct 2011
 Posts
 90
 Rep Power
 0
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.
 10252011, 08:35 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,231
 Blog Entries
 7
 Rep Power
 24
Re: Parameters with two integers.
Change line #26 to:
Java Code:for (int i = 1; i <= x; i++){
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 10252011, 08:38 PM #6Member
 Join Date
 Oct 2011
 Posts
 90
 Rep Power
 0
Similar Threads

how do you add up integers in an array?
By shazakala in forum New To JavaReplies: 7Last Post: 04192011, 10:32 AM 
how to pass parameters from a method to another which accepts to parameters?possible?
By amrmb09 in forum Advanced JavaReplies: 5Last Post: 11212010, 03:08 PM 
Set of Integers
By rsjava24 in forum New To JavaReplies: 7Last Post: 01282010, 11:29 AM 
Integers and Lists
By TGH in forum New To JavaReplies: 8Last Post: 01272010, 10:49 AM 
Random Integers
By www.kwalski.com in forum Java AppletsReplies: 8Last Post: 12092007, 06:49 PM
Bookmarks