Results 1 to 6 of 6
  1. #1
    Rstuart970 is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Exclamation Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK

    Hello, I am currently taking my first java class ever and I have spent hours on trying to figure out what in my code is wrong. I only have until early tomorrow morning to have it complete any help would be much appreciated, I started reading these forums and I think I will be reading them as a part of my daily routine from now on. Now here is my assignment word for word.

    3. (10 pts) Write a program that prompts the user to enter an integer. If the number is a multiple of 5, print HiFive. If the number is divisible by 2 or 3, print Georgia. Here are the sample runs:
    <Output> Enter an integer: 6
    Georgia
    <End Output>
    <Output> Enter an integer: 15
    HiFive Georgia
    <End Output>
    <Output> Enter an integer: 25
    HiFive
    <End Output>



    And here is my code I have for my final. I have a strong feeling it is where I have the final else if trying to print HiFive and Georgia in the case that it is a multiple of 5 and is divisible by 2 or 3. Now in my test run I enter the number 6 which should just print Georgia although, I am getting a print of Georgia and then another print on a new line with HiFive Georgia.



    // Unit 1 Assignment - #3


    import java.util.Scanner;

    public class HiFiveInteger {

    public static void main(String[] args) {


    // Ask for an integer
    Scanner input = new Scanner(System.in);
    System.out.print("Enter an integer: ");
    int num1 = input.nextInt();

    // Check if multiple of 5
    if (num1 % 5 == 0)
    {
    System.out.println("HiFive");

    }

    // Check if integer is a divisible of 2 or 3
    else if (num1 % 2 == 0
    || num1 % 3 == 0)
    {
    System.out.println("Georgia");

    }

    // Check if both are true
    else if (num1 % 5 == 0
    && num1 % 2 == 0 || num1 % 3 == 0);
    {
    System.out.println("HiFive Georgia");

    }

    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK

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

  3. #3
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK

    Java Code:
    else if (num1 % 5 == 0
    && num1 % 2 == 0 || num1 % 3 == 0);
    {
    System.out.println("HiFive Georgia");
    
    }
    what your saying here is if a number divided by 5 and 2 has no denominate then your good

    OR(||)

    if a number divided by 3 has no denominater your good

    6/3 = 2 with no denominater so therefor it prints "HiFive Georgia"


    so the && will make it to where both conditions need to be true

    the || means this or that needs to be true

    so think of it like this
    num1 % 5 == 0 and num1 % 2 == 0 or this num1 % 3 == 0

    hope this helps
    Last edited by killutch; 09-16-2012 at 09:49 AM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK

    The && operator has a higher precedence than the || operator, so A && B || C evaluates as (A && B) || C; that is not what you want, you want to evaluate it as A && (B || C) so you should put parentheses around the subexpression; oh, and get rid of that semi-colon at the end of that if-clause.

    kind regards,

    Jo
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK

    He has had this solved elsewhere in a cross-post but neglected to tell us. Very inconsiderate of him.

  6. #6
    flyingeagle is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default Re: Problem with school assignment. print hifive if multiple of 5 etc. PLZ HELPQUICK

    // Unit 1 Assignment - #3


    import java.util.Scanner;

    public class HiFiveInteger {

    public static void main(String[] args) {


    // Ask for an integer
    Scanner input = new Scanner(System.in);
    System.out.print("Enter an integer: ");
    int num1 = input.nextInt();

    // Check if multiple of 5
    if (num1 % 5 == 0)
    {
    System.out.println("HiFive");

    }

    // Check if integer is a divisible of 2 or 3
    if (num1 % 2 == 0
    || num1 % 3 == 0)
    {
    System.out.println("Georgia");

    }


    }

    }
    The above changed code should to the trick.

Similar Threads

  1. I need help with my school assignment, please
    By burnenator in forum New To Java
    Replies: 13
    Last Post: 11-21-2011, 05:54 AM
  2. Need help with school assignment
    By sljkbn in forum New To Java
    Replies: 6
    Last Post: 10-28-2009, 02:07 PM
  3. Help with noob school assignment
    By debo3381 in forum New To Java
    Replies: 3
    Last Post: 09-23-2009, 05:02 AM
  4. Question about school assignment
    By wata in forum New To Java
    Replies: 7
    Last Post: 08-18-2009, 03:00 PM
  5. Please help... assignment for school
    By confused2000 in forum New To Java
    Replies: 3
    Last Post: 11-12-2007, 09:12 AM

Posting Permissions

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