Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By Junky
  • 2 Post By KimChi93

Thread: Evens odds & likes 7

  1. #1
    Martyn is offline Member
    Join Date
    Nov 2011
    Location
    Belfast
    Posts
    37
    Rep Power
    0

    Default Evens odds & likes 7

    Hello I am fairly new to Java and I have been given a task by my tutor at uni,

    From what I can see I have everything in the corect positions and the even print at the even numbers where they are supposed to and the odd prints at the odd numbers where they are supposed to.

    But for the odd text (i+ " odd and Likes 7" ) i.e. divides by 7 I cant get it to print at the correct odd numbers, it prints directly after the
    (i+ " even and Likes 7" )

    I have pasted my code below this picture


    Evens odds & likes 7-evens-odds-likes-7s.jpg

    /**

    public static void main(String[] args) {

    //Decalre loop limits
    for( int i =1; i<31; i++){

    //Code for odd numbers
    if ((i%2 == 0) && (i%7 != 0)){
    System.out.println(i+ " even");

    // Code for even numbers
    }else if ((i%2 == 1) && (i%7 != 1)) {
    System.out.println(i+ " odd");

    // Code for likes 7
    }else if ((i%2==0) && (i%7==0)){
    System.out.print(i+" even and likes 7");
    System.out.println();

    }else if ((i%2==1) && (i%7==1)){
    System.out.print(i+" odd and likes 7 ");
    System.out.println();

    }
    }
    }//Main
    }//Public Class

    */

    Any help would be greatly appreciated .

    Thanks Martyn

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Evens odds & likes 7

    You need to recheck your conditions for mod 7.

  3. #3
    KimChi93's Avatar
    KimChi93 is offline Programmer
    Join Date
    Nov 2011
    Location
    USA BABY
    Posts
    9
    Rep Power
    0

    Default Re: Evens odds & likes 7

    simple error :D I'm a new programmer too somewhat. Your last else if line that states if it is odd and likes 7 is wrong. change the (i%7==1) to (i%7==0). simply just change the 1 to a 0 and it should work. Let me know if this helped :D

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Evens odds & likes 7

    Was it really necessary to spoonfeed the answer? I could have done that. The point of my post was for the OP to think about the problem and think about a solution themselves.
    Martyn likes this.

  5. #5
    KimChi93's Avatar
    KimChi93 is offline Programmer
    Join Date
    Nov 2011
    Location
    USA BABY
    Posts
    9
    Rep Power
    0

    Default Re: Evens odds & likes 7

    sorry i'm new to the forum and kinda excited to find a bunch of people that have common hobbies as myself. just wanted to help.
    Martyn and pbrockway2 like this.

  6. #6
    Martyn is offline Member
    Join Date
    Nov 2011
    Location
    Belfast
    Posts
    37
    Rep Power
    0

    Default Re: Evens odds & likes 7

    Hello everyone and thanks for the help Ihave managed to figure out the problem and thank you Juncky for not spoon feeding me the answer I dont believe you learn this way. It turns out it was my modulas values however it was the values on the top lines of code as opposed to the bottom lines like I thought.

    Heres a pictureEvens odds &amp; likes 7-evens-odds-likes-7.jpg

    Heres my new code

    public static void main(String[] args) {

    //Decalre loop limits
    for( int i =1; i<31; i++){

    //Code for odd numbers
    if ((i%2 == 0) && (i%7 != 0)){
    System.out.println(i+" even");

    // Code for even numbers
    }else if ((i%2 == 1) && (i%7 != 0)) {
    System.out.println(i+ " odd");

    // Code for likes 7
    }else if ((i%2==0) && (i%7==0)){
    System.out.print(i+" even and likes 7 \n");


    }else if ((i%2==1) && (i%7==0)){
    System.out.print(i+" odd and likes 7 \n ");

    }
    }
    }//Main
    }//Public Class

    Thanks again everyone

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

    Default Re: Evens odds & likes 7

    @KimChi93: welcome to you, and your enthusiasm! The tradition, or ethos, here is to help a poster to learn or understand, but not to spoon feed code: something which effectively *stops* them develop understanding. Someone is sure to draw attnetion to it if you do that - but I hope that doesn't stop you doing what you can, and posting your own thoughts whereever you feel they might be useful.

    (Sometimes where I can see exactly what the problem is, but a hint or nudge has been given to the original poster, I find myself thinking *about* the problem. In this case, would allowing the trial numbers to be integers rather than whole numbers make a difference to how the code would be written?)

Similar Threads

  1. 1 New Likes Received
    By JosAH in forum Suggestions & Feedback
    Replies: 9
    Last Post: 07-23-2011, 10:56 PM

Tags for this Thread

Posting Permissions

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