Results 1 to 4 of 4
  1. #1
    Avion is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default Ternary Code Help

    I'm still a little confused on the question mark and colon sign. I've already seen threads on how to use it but using it in this array is a little more confusing. I would like it if someone could recode this method using ifs and elses.

    Java Code:
    	private void drawTurtle() {
    
    		for (int i = 0; i < 20; i++) {
    
    			for (int j = 0; j < 20; j++)
    
    				System.out.print(floor[i][j] ? "* " : "  ");
    
    			System.out.println();
    		}
    	}

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

    Default Re: Ternary Code Help

    Java Code:
    System.out.println(foo ? bar : baz);
    means the same as

    Java Code:
    String toPrint;
    if(foo) {
        toPrint = bar;
    } else {
        toPrint = baz;
    }
    System.out.println(toPrint);
    You can try this substitution with your code. One thing to watch is how the braces work: use braces with all structures, even one line for loops.

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Ternary Code Help

    Using if-else statement it will be like what pbrockway2 said. Sorry I couldn't delete my post :D

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

    Default Re: Ternary Code Help

    Moved from Advanced Java.

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

Similar Threads

  1. ternary in the condition of a for loop
    By mikehaas763 in forum New To Java
    Replies: 0
    Last Post: 05-08-2012, 07:35 PM
  2. Question about ternary operators.
    By Harrison in forum New To Java
    Replies: 6
    Last Post: 11-12-2011, 07:54 AM
  3. Shorthand ternary operator ?
    By Billywizz in forum New To Java
    Replies: 3
    Last Post: 10-02-2011, 09:57 PM
  4. doubt regarding ternary operator (?)
    By subith86 in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 08:15 AM
  5. Use of ternary
    By wulfgarpro in forum New To Java
    Replies: 1
    Last Post: 09-21-2010, 06:31 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
  •