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.
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;
}
}
I have another question too.
Lets say i have a pattern like this
Code:
z
zzz
zzzzzzz
zzzzzzzzz
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.
Thanks in advance
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,
Jos
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.
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,
Jos
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.
Re: I don't understand what's wrong with this !
Quote:
Originally Posted by
thirdage
Thanks a lot :) but can you help me with the second one now cause I couldn't manage to write it.
Just a few observations:
1) lines start at line #1
2) the first line has 1 Z
3) even numbered lines have 2 more Zs than the previous line
4) odd numbered lines have 4 more Zs than the previous line.
kind regards,
Jos
Re: I don't understand what's wrong with this !
...and also the spaces.
Each line has (numberOfZsInTheLastRow - nrOfZsInTheCurrentRow) / 2 spaces on each side.
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.
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");
}
So what do you think ? and how could i improve it , shorten it maybe. Thanks in advance :)
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:
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();
}
}
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 ...
kind regards,
Jos
ps. I just guessed an initial value for the number of spaces s.
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 .
Re: I don't understand what's wrong with this !
Quote:
Originally Posted by
thirdage
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 .
I saw a couple of increments (++) in your code and you don't need any if statements as you can see from your (and my) code ...
kind regards,
Jos
Re: I don't understand what's wrong with this !
Quote:
Originally Posted by
thirdage
Hello :)
I was writing this code as a part of my assignment. I wanna know what's wrong with it.
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;
}
}
ur code should have-
boolean val=Leap5.isLeap(year);
instead of isLeap(year);
and year must be declared outside the for loop