Reputation: 85
Im only started learning java. Im practicing on how to print and half integer
I have define the main method and for the halfing method I have also define it in the same class but it wont compile. Any suggestions? this is my code
public class IntAndHalf {
public static void main(String[] args) {
double y;
for (int i = 1; i <= 20; i++) {
System.out.println(i + " " + y);
}
}
public static double halfOfint(int i){
for (i = 0; i <= 20; i++){
y = i/2;
return (y);
}
}
}
Upvotes: 0
Views: 356
Reputation: 59978
Hint
You have a lot of problem in your code :
y
is not define in your main methodi
is already declared in your method, so you have not
to declare it again in your loop (int i = 0;)
just use it (i = 0;)
.;
in the end of y = (i/2)
;
in the end of your loop, the next block
will not executed for(int i = 1; i <= 20; i++ );
return(y)
in the end of your method not in your
loopCorrect this typos and your code will compiled, another thing you never call your method HalfOfInt so don't wait to get information from this method, you have to call in your main method.
Edit
Your code in this case should look like this:
public static void main(String[] args) {
for (int i = 1; i <= 20; i++) {
System.out.println(i + "-" + HalfOfInt(i));
// ^-----------call your method which
//take an int and return the (i/2)
// ^-------------------------Print your value
}
}
public static double HalfOfInt(int i) {
return (double) i / 2;//<<----------- no need to use a loop just return (i/2)
}
Upvotes: 1
Reputation: 5695
Multiple Problems :
;
after your for
loop.y
is not defined in your main method.;
after y = (i/2)
HalfOfInt
i
twice.Also, you should stick with Java naming conventions. Methods should begin with a lower-case letter.
Upvotes: 0