Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to create this conditional expression in Java...?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    My Mood
    Cold
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to create this conditional expression in Java...?

    ok i have a computer science assignment due soon and its concerning java. for the assignment, we must make flags representing different countries. for example, i have to make the italian flag so i need to create a grid thats one thirds green, one thirds white and one thirds red to represent that flag. the user will input a certain width and height that are going to be used to create the grid. the project guidelines have certain conditions that we must add in order for the flag to be shown in output. if the conditions in the guidelines arent valid, i would have to create an error flag (which i know how to do). like here is a condition for the italian flag i just mentioned:

    Its height must be a multiple of 3, and its width must be exactly twice as large as its height to be valid.

    how would i format that into a conditional. i dont get how to make an if statement (or whatever) for a multiple. let me give you a very brief/simple explanation of my code so far:

    Print: "Choose 1 for Italy, 2 for Chile, 3 for Switzerland..."
    choice.scan nextINT()
    Print: "enter width and height"
    width.scan nextINT()
    height.scan nextINT

    // width and height are my 2 variables

    if (choice == 1) {
    /* in this area i have my code to create the flag based on the requirements (1/3 is green, 1/3 is white...)
    */
    }
    (end)

    so i am guessing i need an if statement inside that first if statement with an expression representing those guidelines i mentioned above. (here they are again: its height must be a multiple of 3, and its width must be exactly twice as large as its height to be valid). and then i figure i need to put an else statement to the new if statement that will produce the error flag if that condition i need help with is not true. so yeah. i hope that wasnt too confusing. pretty much i need to figure out how to express this statement with logical operators:
    its height must be a multiple of 3, and its width must be exactly twice as large as its height to be valid.
    the multiple part is confusing me the most. thanks guys.

    i'm guessing the part where it says width must be exactly twice as large as its height will look like this:
    if ( width = height * 2)
    and the multiple part must be an && statement attached to that if condition i wrote.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: How to create this conditional expression in Java...?

    Take it one piece at a time. Focus one one part of one if statement. Ask a specific question about that one specific piece, and it will be easier to help you.

    Hint: A single '=' is assignment. A double "==" checks for equality.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Basic Math Expression Java Problem
    By andyluvskrissy in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 15th, 2011, 02:22 AM
  2. Replies: 3
    Last Post: September 30th, 2011, 08:45 AM
  3. Conditional statements on Array list objects
    By jaguarpaw in forum Collections and Generics
    Replies: 3
    Last Post: May 10th, 2011, 09:52 AM
  4. Basic Math Expression Java Problem
    By andyluvskrissy in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 02:46 PM
  5. Using Regular Expression (regex) in Java Programming
    By lordelf007 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 14th, 2010, 10:29 AM