Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
youssef elmaraghy
140 Pointshow to fix that code, i can't run it, something missing
class basic
{
public static void main(String ar[])
{
int i;
for (i=1;i<=200;i++)
{
if(i%3==0 && i%5!=0 )
System.out.println(i+"my");
else if(i%5==0 && i%3!=0)
System.out.println(i+"name");
else if(i%3==0 && i%5==0)
System.out.println(i+"my name");
else
System.out.println(i);
}
}
}
4 Answers
Mohsin Ayub
6,822 PointsYour main() function declaration is wrong, the correct syntax is:
public static void main(String[] args) {
}
youssef elmaraghy
140 PointsI added the changes but it's still not working.
can you add all together please
class basic
{ public static void main(String[] args)
{
int i; for (i=1;i<=200;i++)
{
if(i%3==0 & i%5!=0 )
System.out.println(i+"net");
else if(i%5==0 && i%3!=0)
System.out.println(i+"rix");
else if(i%3==0 && i%5==0)
System.out.println(i+"netrix");
else
System.out.println(i);
} } }
Mohsin Ayub
6,822 PointsDid you create this program in workspaces and trying to run it in workspaces?
youssef elmaraghy
140 Pointsin jdk
Mohsin Ayub
6,822 PointsIt works for me. I pasted the same code that you give to me and it works fine. Now i don't know what is the problem and what are the errors because the program successfully run in my editor. I am using Eclipse.
Gloria Dwomoh
13,116 PointsYou have some errors in your code. The curly braces are missing in the if else (some are misplaced as well), also the class name should begin from uppercase letter. I solved the question (I have not 100% tested it since It is super late on my side but it does what you asked I believe) as I said...the braces are your main error.
public class Basic{
public static void main(String []args){
int i;
for (i=1;i<=200;i++)
{
if(i%3==0 && i%5!=0 ){
System.out.println(i+" my");
}
else if(i%5==0 && i%3!=0){
System.out.println(i+" name");
}
else if(i%3==0 && i%5==0){
System.out.println(i+" my name");
}
else{
System.out.println(i);
}
}//end for loop
}//end main
}//end class
I ran it on an online compiler but it should work on the software you use as well. Please pay close attention to the curly braces I added and the public static void main part.
youssef elmaraghy
140 Pointsyoussef elmaraghy
140 Pointsscript that will iterate through the numbers 1-200
Next to each number that is divisble by 3 ouput to the console "my"
Next to each number that is divisble by 5 output to the console "name"
For each number that is divisible by 3 AND 5 output "my name"
Example Output:
1
2
3 my
4
5 name
.
15 my name