Results 1 to 4 of 4
  1. #1
    deussoj is offline Member
    Join Date
    Dec 2016
    Posts
    2
    Rep Power
    0

    Post How can I shorten this?

    How can i shorten the number of lines of code? This is to calculate the amount spent each day for the 12 days of christmas using NetBeans. This is the best I could do and dont know how to shorten/simplify it from here

    Java Code:
    public static void main(String[] args) {
    int[] costsarr = {210, 375, 181, 600, 750, 360, 13125, 58, 7553, 5509, 2708, 2934};
    System.out.println("Cost per day");
    for (int y = 0; y < costsarr.length; y++) {
    
    int sum = 0;
    
    if (y == 0) {
    
    sum = sum + costsarr[0];
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    
    if (y == 1) {
    
    sum = sum + costsarr[0] + costsarr[1] * 2;
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    
    if (y == 2) {
    
    sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3;
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    
    if (y == 3) {
    
    sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4;
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    
    if (y == 4) {
    
    sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5;
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    if (y == 5) {
    
    sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6;
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    if (y == 6) {
    
    sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7;
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    if (y == 7) {
    
    sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8;
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    if (y == 8) {
    
    sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9;
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    if (y == 9) {
    
    sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9 + costsarr[9] * 10;
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    if (y == 10) {
    
    sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9 + costsarr[9] * 10 + costsarr[10] * 11;
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    if (y == 11) {
    
    sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9 + costsarr[9] * 10 + costsarr[10] * 11 + costsarr[11] * 12;
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    if (y == 12) {
    
    sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9 + costsarr[9] * 10 + costsarr[10] * 11 + costsarr[11] * 12 + costsarr[12] * 13;
    
    System.out.println("Day " + (y + 1) + ": $" + sum);
    }
    }}}
    Last edited by deussoj; 12-08-2016 at 12:42 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How can I shorten this?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Be sure the code is properly indented. Code without indentations is harder to read and understand.

    Why have a loop? Just have the statements one after the other. Get rid of the if statements with the y == tests
    Last edited by Norm; 12-08-2016 at 12:34 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How can I shorten this?

    Java Code:
          int sum = costsarr[0];
          System.out.println("Day 1: $" + sum);
          for (int y = 1; y < 12; y++) {
             sum += costsarr[y] + costsarr[y]*y;
             System.out.println("Day " + (y+1) + ": $" + sum);
          }
    You could eliminate line 2 and init sum to 0 and put the whole thing in a loop. But then you do
    a useless multiply by 0 which doesn't sit well with me.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    deussoj is offline Member
    Join Date
    Dec 2016
    Posts
    2
    Rep Power
    0

    Default Re: How can I shorten this?

    Quote Originally Posted by jim829 View Post
    Java Code:
          int sum = costsarr[0];
          System.out.println("Day 1: $" + sum);
          for (int y = 1; y < 12; y++) {
             sum += costsarr[y] + costsarr[y]*y;
             System.out.println("Day " + (y+1) + ": $" + sum);
          }
    You could eliminate line 2 and init sum to 0 and put the whole thing in a loop. But then you do
    a useless multiply by 0 which doesn't sit well with me.

    Regards,
    Jim
    Thank you so much! this helped me a lot and now I understand.

Similar Threads

  1. shorten code
    By Hypenate in forum New To Java
    Replies: 1
    Last Post: 01-29-2008, 08:04 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
  •