Results 1 to 6 of 6
Thread: Parameters with two integers.
- 10-25-2011, 02:53 PM #1
Member
- 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; 10-25-2011 at 03:16 PM.
- 10-25-2011, 05:49 PM #2
Member
- 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.
- 10-25-2011, 08:24 PM #3
Member
- 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.
Is there a better way / less code etc.?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
- 10-25-2011, 08:31 PM #4
Member
- 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.
- 10-25-2011, 08:35 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Parameters with two integers.
Change line #26 to:
Your version forgot that last number (and you don't have to print it in your main( ... ) method as an afterthought)Java Code:for (int i = 1; i <= x; i++){
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-25-2011, 08:38 PM #6
Member
- 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: 04-19-2011, 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: 11-21-2010, 02:08 PM -
Set of Integers
By rsjava24 in forum New To JavaReplies: 7Last Post: 01-28-2010, 10:29 AM -
Integers and Lists
By TGH in forum New To JavaReplies: 8Last Post: 01-27-2010, 09:49 AM -
Random Integers
By www.kwalski.com in forum Java AppletsReplies: 8Last Post: 12-09-2007, 05:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks