Results 1 to 5 of 5
- 10-05-2011, 03:56 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Need help with methods and 'for' loops?
The method evenSquares takes a single int parameter, n, (for example, 10000), and then prints all of the (positive) even perfect squares less than n, each on a separate line. Notice that evenSquares has a void return type, since all it does is print integers to the console. Be sure to use the println method to print each entry of your output.
Example: if n = 40, your code should print
4
16
36
(Hint: your method should be built around a for loop with a test component that asks if the square of the control variable (say, k) is < n. Thus, the loop should terminate as soon as k*k equals or exceeds n.)
---
I had gotten this:
public void evenSquares(int n){
int squares = k*k;
for(int i=0; i<=n; i++){
System.out.print(squares);
}
}
--
It says that it can't find "variable k" but I'm not quite sure what I'm doing wrong and where to fix it :$ Any help would be greatly appreciated!
- 10-05-2011, 04:31 AM #2
Member
- Join Date
- Jun 2011
- Posts
- 46
- Rep Power
- 0
Re: Need help with methods and 'for' loops?
where is 'k' declared as a variable? Do you have the statement
int k;
or
int k = 0;
- 10-05-2011, 04:39 AM #3
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 10
Re: Need help with methods and 'for' loops?
dragstang86 is right, you need to declare a variable before you use it.
- 10-05-2011, 06:28 AM #4
Re: Need help with methods and 'for' loops?
you should write like followings:
Snip: No spoon feeding. You have been warned multiple times. Please stop.Last edited by sunde887; 10-05-2011 at 11:27 PM.
- 10-05-2011, 11:25 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-17-2012, 12:00 AM -
Incorporating If-Else into Methods + Private Helper Methods?
By 5minutes in forum New To JavaReplies: 1Last Post: 10-05-2011, 01:15 AM -
Combination of loops, arrays, methods, anyone? need help badly!
By Maximuz313 in forum New To JavaReplies: 8Last Post: 03-28-2011, 01:06 PM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 05:02 AM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 07:36 AM
Bookmarks