Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By CIT_peter

Thread: Help

  1. #1
    CIT_peter is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default 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.

    ** This space for rent **

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default 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 
            	}
            }  
    	}
    }

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help

    Quote Originally Posted by cselic View Post
    @CIT_Peter:
    Here is one example...
    [/code]
    I don't think an if-else if chain will have the desired effect. And your sc is cruft in a situation crying out for care, precision and thought.

    Possibly when (or if) the OP responds with some further thoughts and code we'll have more to work on.
    Last edited by pbrockway2; 06-30-2012 at 01:39 AM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Help

    CIT_peter, please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    CIT_peter is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help

    thank you so much everybody for helping me...
    and sorry DarrylBurke...that is my 1st thread here..it wont happen again...
    DarrylBurke likes this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •