Any better solution for this ?

public static void main (String args [])
     for (int row = 1; row <= 5; row++)
          for (int col1 = 4; col1 >= row; col1--)
               System.out.print(" ");
          for (int col2 = row*2; col2 > 1; col2--)
               System.out.print("*");
          System.out.println();
     for (int row = 1; row <= 4; row++)
          for (int col1 = 1; col1 <= row; col1++)
               System.out.print(" ");
          for (int col2 = 4; col2 >= row; col2--)
               System.out.print("*");
          for (int col3 = row; col3 < 4; col3++)
               System.out.print("*");
          System.out.println();
}I'm trying to make this:
I think that my solution is too long. any shorter solution?
TQ

It IS hardwired to produce that figure and only that figure.
What if they asked you to redo it with fewer or more rows of stars?
You did a "bad" thing: you hardwired your loop sizes. Something like "i <= 4" is called a "magic number", because someone reading your code would have no idea where the 4 came from.
You also have 4 appear in several places. That violates the Don't Repeat Yourself rule. If you have to change it, and forget one of the spots, your code is wrong.
You can help both situations by defining a well-named constant and using that for the loop upper limit:
public class DiamondsAreAGirlsBestFriend
   public static final int DIAMOND_SIZE = 4;
   public static void main(String [] args)
      for (int row = 0; row <= DIAMOND_SIZE; ++row)
      // and so on
}Also, better to put this into a method that would take in a diamond size value and return a String. Then it's general.
None of this is very important for this trivial problem, but they might be worth remembering when you start writing more complex programs.
%

Similar Messages

  • Any Sql Solution for this?

    Hi guys,
    How can I update rows which total upto the number of dollars the user enters.
    Ex: I have the following rows in the table
    Col1 Col2 Col3 Col4
    1------10------A
    2-------3------B
    3-------7------C
    4------20------D
    5-------5------E
    6-------5------F
    I have a procedure which takes a user input (say 23). Now I need to update Col4 with EXT for all those rows which total to 23. So the result should look like
    Col1---Col2---Col3----Col4
    1------10------A------EXT
    2-------3------B------EXT
    3-------7------C
    4------20------D
    5-------5------E------EXT
    6-------5------F------EXT
    Had to insert dashes for formatting purposes...
    Any solutions will be appreciated.
    Thanks
    John

    Hi John,
    Well, that's a very strange way to do it but if you must, there's no problem saying:
    UPDATE TABLE SET Col4 = 'EXT' WHERE Col1 + Col2 + Col3 = 23;
    However, I suspect you intend that the user may enter any number of values. If this is the case, you'd be much better off (in the relational world, anyway) entering a new row for each of the new values, viz:
    User, Date, Input
    1, 01/01/2003, 10
    1, 13/01/2003, 5
    1, 15/01/2003, 8
    2, 19/01/2003, 20
    2, 10/02/2003, 3
    3, 4/01/2003, 21
    Now you can say:
    SELECT SUM (Input) Total, COUNT (Input) NrInputs FROM TABLE GROUP BY User HAVING SUM (Input) = 23;
    ... which works much better and will work for any number of columns. You don't even need the date column unless, for some reason, you need to order the input.
    d.

  • Any help/solution for this please??

    You'll need to subscribe to watch this channel.
    I see it looks like a lot of people may be having this issue, i now cannot view any of the BT Sports channels beit in HD or normal.
    Black screen telling me i have to subscribe.
    Went on live chat 87 in the queue, by the time i got down to number 1 (30plus minutes) it said there was no available advisors.
    I was also on the phone the same length of time, only for it to go dead, this has happened 3 times whilst trying to phone BT.
    Any help please, thanks

    I can view it all via the apps and the online player, just not via my BT Vision box.

  • Hello is there any better view for this code?

    thanks
    about_btn.addEventListener(MouseEvent.MOUSE_DOWN, aboutclick);
    function aboutclick(event:MouseEvent) {
    gotoAndPlay("page2");
    gallery_btn.addEventListener(MouseEvent.MOUSE_DOWN, gallery_click);
    function gallery_click(event:MouseEvent) {
    gotoAndPlay("page3");
    video_btn.addEventListener(MouseEvent.MOUSE_DOWN, video_click);
    function video_click(event:MouseEvent) {
    gotoAndPlay("page4");
    or_mc.addEventListener(MouseEvent.MOUSE_DOWN,drag);
    or_mc.addEventListener(MouseEvent.MOUSE_UP,drop);
    function drag(event:MouseEvent):void{
        or_mc.startDrag();
    function drop(event:MouseEvent):void{
        or_mc.stopDrag();
    all.pah.addEventListener(MouseEvent.MOUSE_DOWN,drag1);
    all.pah.addEventListener(MouseEvent.MOUSE_UP,drop1);
    function drag1(event:MouseEvent):void{
        all.pah.startDrag();
    function drop1(event:MouseEvent):void{
        all.pah.stopDrag();

    What do you mean?  Except for what appears to be an excess curly brace at the end, and an inconsistent use of ":void" in the function declarations and indentations too, it looks okay functionally.

  • Is there any alternative logic for this issue

    Hello all ,
    I have a  program which is executed as a background job for every 15 minutes . Now my requirement is If this batch job is successfully finished, i have to capture that time and date at which the batch job is successfully finished and update the corresponding time and date in a ZTABLE .
    In order to achieve this , i have created a separate program which will also be running in background.in this program , using the table TBTCO, i m getting the last date and  last time at which the batch job is successfully finished and modifying the ZTABLE accordingly
    Is there any alternate solution for this ?
    thanks&regards,
    G.Pavan

    Hi gpavansap,
    When you define background job in SM36, your first step should be the program which you have scheduled now.
    Create a second step in the same job with another program which would just update system date and time to the ZTABLE. No need to read TBTCO and all.
    The second step would immediately execute as soon as the first step ended successfully.
    If you don't want to create a separate program. you can have a check-box "Update ZTable Only" in the selection screen of the first program itself. If this check-box is selected, the program should only update ZTABLE and do nothing else. Create a variant of your selection screen with this check-box selected and schedule this variant as "Second Step" of your back-ground job.
    Regards
    Suresh

  • My boss is using Mac machine, whenever I send email from my windows machine he receives email as winmail.dat, is there any solutions for this ?

    My boss is using Mac machine, whenever I send email from my windows machine he receives email as winmail.dat, is there any solutions for this ?
    My boss is using Mac machine, whenever I send email from my windows machine he receives email as winmail.dat, is there any solutions for this ?

    Upgrading my work PC is NOT an option, but I'd like my Mac to be able to recognise the attachment for what it is
    Let see, you are using a proprietary file format that is only supposed to work with Outlook. It doesn't work for millions of Windows or Mac or Linux email users that depend on other email programs like Outlook Express, Thunderbird, Apple Mail, webmail, etc.
    Microsoft has had to publish instructions so that their program's users can make themselves compatible with the rest of the world.
    http://support.microsoft.com/kb/290809
    You are causing your own problem by not making a simple change in your program's options. Changes in the Outlook preferences can be made either Globally or by the Recipient.
    Follow these steps in Outlook 2007 to turn off TNEF:
    1. On the Tools menu, click Options.
    2. Click the Mail Format tab.
    3. In the Compose in this message format box, click Plain Text or HTML, and then click OK.
    Don't know of a better way to fix this problem for everyone concerned. Even if you get the fix to work with your machine, you will not enable all of the people receiving your emails to be able to work with you. That's a losing battle that's been caused by Microsoft.
    Now if Microsoft created a Mail plugin for other email programs like they do for their video format's and services (flip4mac, Office Live Plugin, etc), then I would consider using their email program to deal with my clients. Until then, I need to be compatible with many different programs in other countries, on different platforms. Maybe that's going to change when they bring out the new Office 2011 with Outlook for the Mac...

  • After update my iPad 2 with IOS 6.0.1 the botton mute and sound + doesn't work...any solution for this??? Thanks

    After update my iPad 2 with IOS 6.0.1 the botton mute and sound + doesn't work...any solution for this??? Thanks

    There are numerous reports of this issue happening on iPhones. It may be a glitch that will need a new update. Some find that a full shut-down and restart fixes that problem, but not always.

  • I bought iPhone 3GS(Used) recently and updated ios 6.1.3 my iPhone is taking blurry Pictures through basic default Cam app.Can anybody please let me know any solution for this?

    I bought iPhone 3GS(Used) recently and updated ios 6.1.3 my iPhone is taking blurry Pictures through basic default Cam app.Can anybody please let me know any solution for this?

    Basic troubleshooting steps right out of the user guide are restart, reset, restore from backup, restore as new.  If you've been through ALL the steps and you still have the problem, then you'll need to bring your phone to Apple for evaluation.

  • I bought my iphone 5 in Houston Texas May 15 2013 IMEI Nr. 013428009645399.The problem is that in the Greece the country which I live the 4G is not working.If you have any solution for this problem pls. let me know.My email is philcoueth@yahoo.gr Thank yo

    I bought my iphone 5 in Houston on May 15 2013.
    IMEI 013428009645399.The problem I have is that in the country
    which I live GREECE the 4G is
    not working.Please if you have any solution for this
    problem let me know.My email is [email protected]
    Thanking you in advance
    Philip Couridis

    iPhones purchased in the US are NOT guaranteed to work with 4G bands outside of North America.
    For what crazy reason did you purchase an iPhone in the US if you live in Greece?  If your phone needs servicing, it will have to be brought back to the US.  You cannot get that phone serviced in Greece.

  • I just bought a new iMac and am having troubles downloading creative cloud installer.  Its coming up with error 214 - I can't find any solutions for this.  Can anyone help me?  Im on mac OS 10.9.4

    I just bought a new iMac and am having troubles downloading creative cloud installer.  Its coming up with error 214 - I can't find any solutions for this.  Can anyone help me?  Im on mac OS 10.9.4

    Meg81 error 214 indicates a download data validation error.  This means that the update you downloaded became corrupted likely during the download process.  I would recommend utilizing the suggestions listed in Error downloading Creative Cloud applications - http://helpx.adobe.com/creative-cloud/kb/error-downloading-cc-apps.html to attempt to improve the stability of your Internet connection.
    You can also find information on how to review your download logs which may provide additional information regarding the failure of the download.

  • When i tap "update" on an app, or try and download a new app i get the loading wheel for around a second then it goes back to showing "update" and does nothing.  Any solutions for this?

    When i tap "update" on an app, or try and download a new app i get the loading wheel for around a second then it goes back to showing "update" and does nothing.  Any solutions for this?  This is relevant to an iPhone 5S running iOS 8

    If you're really running iOS 8, you should be posting in the Developer's Forum, as you well know since you signed an NDA when you signed up for the Developer's program.

  • I accidently removed my clash of king game from Game Center, but my profile is still there and i want it back. Any solution for this situation?

    I Accodently removed mu Clash of King game from Game center, but my profile is still there, I want it back.Any solution for this situation?

    Install the game again.

  • I have a problem openning my Itune home, I'm getting unknown error 0x80090308, any body has a solution for this problem

    I have a problem openning my Itune home, I'm getting unknown error 0x80090308, any body has a solution for this problem

    hi marco
    I found the problem. Do you have other wi-fi (w-lan) like repeater or tv-transmitter or d-link etc. if yes, please disconnect them once and try again after restarting the time-capsule resp. change the channel if you can to another one. then it should work again.
    @ home i have a tv-transmitter via wi-fi and indeed, i changed the channel from 1 to 6 or something like that, then the iphone worked again.

  • M having a macbook, after successful shut down it is booting up automatically is there any solution for this?

    M having a macbook, after every successful shut down it is booting up automatically. Is there any solution for this?

    There isn't a separate app icon for Facetime for the iPhone like there is for the iPad and iPod Touch.  Facetime is built into the iPhone's iOS.
    Here a refresher on how it works: 
    iOS:  Using Facetime, http://support.apple.com/kb/ht4319

  • Buy second hand iPhone ........... i have apple id of previous owner but no password ... now the phone is in icloud activation mode . any solution for this.

    i buy second hand iPhone from Olx ........... i have apple id of previous owner but no password ...
    now the phone is in icloud activation mode . any solution for this.
    totally wastage of money , if there is no solution .
    for security purpose this icloud activation is Good ............

    Read this
    http://support.apple.com/kb/TS4515?viewlocale=en_US&locale=en_US

Maybe you are looking for

  • Calendar Settings

    I recently returned my Droid and got an iPhone. I like the iPhone, but I've quickly noticed it is short on tweaking and extra settings. My biggest concern right now is the lack of calendar settings. For example, I want to set my calendar so that ther

  • Is there a scrollbar feature in the Music app on my iPod?

    I have a few audio books that I'm listening to and each 'track' is well over an hour long. Annoyingly, every now and then my iPod loses it's spot and it starts me from the beginning again, so I need to hold my finger on the fast-foward screen button

  • [SOLVED Thanks ]// problem with loading XML in projectors and Firefox

    Hi ! I've been working and testing my movie in flash only. when I start the file in IE it works but when I try to load the file in firefox or make a standalone exe player it won´t read the XMLfile. I've tried to allow all access and set the root in t

  • How do I bypass ad to get to App store

    When I tap "App Store", I get a Groupon,Inc add, and can't get any further unless I tap "free" which I don't want to do. Can I bypass this?

  • Photoshop Elements 10 Printing a Photo

    When I open a photo from one of my files in PE10 and want to print it at 4x6 (which I properly indicate in the appropriate box,) it will do that if I keep the photo centered on the page.  But if I move the photo to the top of the page so that I can p