Results 1 to 7 of 7
- 06-29-2012, 10:51 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
Help
Hello Sirs, good day....I am new to java and i have some little difficulty of solving simple java programs...
I have an activity and it requires a 50 integer for loop and when ever an integer is divisible by 3 it outputs Hello , if its divisible by 5 it outputs world , if it is divisible by both 3 and 5 it outputs Hello World...the program should output
1
2
3 Hello
4
5 World
6 Hello
7
8
9 Hello
10 World
.....
15 Hello World
this is my code and i need help on where i am wrong...i would really appreciate it if anyone helps...thank you
Java Code:import java.util.*; class Exer13{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int i; for(i=1;i<=50;i++) System.out.println(+i); { do{ System.out.println(+i+" Hello World"); } while((i%3!=0)&&(i%5!=0)); } } }Last edited by CIT_peter; 06-29-2012 at 10:58 AM.
- 06-29-2012, 11:43 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Help
You don't use sc anywhere, so delete it. In general it's not a good idea to declare variables unless (and until) you want to use them.
Consider using if statements instead of the do-while loop.
- 06-29-2012, 01:40 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,440
- Rep Power
- 16
Re: Help
Also you need to sort your indenting out.
That might highlight a problem with your 'for' loop.Please do not ask for code as refusal often offends.
- 06-29-2012, 03:00 PM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Re: Help
@CIT_Peter:
Here is one example how your code for solution of your problem could look like:
Java Code:import java.util.Scanner; public class Exer13 { public static void main(String[] args){ Scanner sc=new Scanner(System.in); int i; for(i=1;i<=50;i++) { System.out.println(i); if(/* integer is divisible by 3 - write your code here */) { System.out.println("Hello"); // this is output Hello } else if(/* integer its divisable by 5 - write your code here */) { // output World - write your code here } else if(/* integer is divisable by both 3 and 5 output Hello World* - write your code here */) { // output Hello World - write your code here } } } }
- 06-30-2012, 01:35 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Help
Last edited by pbrockway2; 06-30-2012 at 01:39 AM.
- 06-30-2012, 11:39 AM #6
Re: Help
CIT_peter, please go through the Forum Rules -- particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 07-05-2012, 06:51 AM #7
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks