Results 1 to 12 of 12
- 10-22-2011, 07:03 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
I don't understand what's wrong with this !
Hello :)
I was writing this code as a part of my assignment. I wanna know what's wrong with it.
I have another question too.Java Code:package asst3; public class Leap5 { public static void main(String[] args) { for (int n=1; n<=5; n++) { int year = Integer.parseInt(args[n]); } isLeap(); } public static boolean isLeap(int year) { boolean leap = ((year % 4 ==0) && (year % 100 != 0))||(year % 400 == 0); return leap; } }
Lets say i have a pattern like this
where the second row have 2 Zs more than the first ... and the third has 4 Zs than the second... and the fourth has 2 Zs more than the third .. and the fifth has 4Zs more than the fourth. I wanna understand how to do this with If , else statements.Java Code:z zzz zzzzzzz zzzzzzzzz
Thanks in advance
- 10-22-2011, 07:13 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Re: I don't understand what's wrong with this !
According to you there is something wrong with your code? Care to explain why you think anything is wrong with your code? Just telling us that something is wrong is too vague.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-22-2011, 07:20 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
Re: I don't understand what's wrong with this !
Well Eclipse is showing an error on the 12 th line ... the problem is with isLeap() . should i insert something inside the parenthesis? Am i calling the method in a correct manner?
hope everything is okay now.
Thanks, thirdage.
- 10-22-2011, 07:26 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Re: I don't understand what's wrong with this !
The method isLeap( ... ) also takes a parameter; you are trying to call it without a parameter in line #12 and Eclipse protests against it (and rightly so).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-22-2011, 07:34 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
Re: I don't understand what's wrong with this !
Thanks a lot :) but can you help me with the second one now cause I couldn't manage to write it.
- 10-22-2011, 07:46 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Re: I don't understand what's wrong with this !
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-22-2011, 09:15 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Re: I don't understand what's wrong with this !
...and also the spaces.
Each line has (numberOfZsInTheLastRow - nrOfZsInTheCurrentRow) / 2 spaces on each side.
- 10-23-2011, 12:05 AM #8
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
Re: I don't understand what's wrong with this !
I came up with something like this, but i think i wrote too much..for something that could be done in few lines.
So what do you think ? and how could i improve it , shorten it maybe. Thanks in advance :)Java Code:for (int c=1; c<=(rows/4-2); c++) { for (int d=1; d<=6*c-5; d++ ) { System.out.print("Z"); } System.out.println(""); for (int e=1; e<=6*c-3; e++) { System.out.print("Z"); } System.out.println(""); } for (int f=1;f<=(6*(rows/4-1)-5); f++) { System.out.print("Z"); }
- 10-23-2011, 08:49 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Re: I don't understand what's wrong with this !
That looks a bit too complated for me; The increment of the number of z's is 2, 4, 2, 4 etc. starting with 2. The number of z's starts with 1 z. For a value of the increment, the increment for the next line is 6-increment. The number of leading spaces starts with s and is decremented by increment/2 steps. This leads to the code:
Maybe it can be done even more terse, but I haven't had my coffee yet so I wouldn't know ... if you take out the actual printing of a bunch of characters on a line (as I did) you can shorten your code even more ...Java Code:private static void print(char c, int n) { for (int i= 0; i < n; i++) System.out.print(c); } public static void main(String[] args) { for (int line= 1, z= 1, inc= 2, s= 10; line <= 6; line++, z+= inc, s-= inc/2, inc= 6-inc) { print(' ', s); print('Z', z); System.out.println(); } }
kind regards,
Jos
ps. I just guessed an initial value for the number of spaces s.Last edited by JosAH; 10-23-2011 at 08:52 AM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-23-2011, 11:01 AM #10
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
Re: I don't understand what's wrong with this !
Really thanks a lot Mr.JosAH.
But there's still one problem. I want this to be done using If statements & loops, since i haven't took increment yet. I know am starting to be a pain in the *** but I couldn't reall figure out how to do this by myself .
- 10-23-2011, 11:07 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Re: I don't understand what's wrong with this !
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-24-2011, 08:24 PM #12
Member
- Join Date
- Jul 2011
- Location
- New Delhi,India
- Posts
- 56
- Rep Power
- 0
Similar Threads
-
GUI help. Don t understand
By s0meb0dy in forum AWT / SwingReplies: 2Last Post: 10-27-2010, 09:40 PM -
Trying to understand
By ladykrimson in forum New To JavaReplies: 20Last Post: 10-12-2010, 11:10 PM -
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM -
I don´t understand
By Manikyr in forum New To JavaReplies: 6Last Post: 02-22-2009, 11:22 PM -
Cannot understand whats wrong
By Lehane_9 in forum New To JavaReplies: 1Last Post: 03-06-2008, 07:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks