Decompose a number in ones with problem in the deployment

0

I have made a program in java, which decomposes in a sum of numbers some, to a value x, which is entered by the user. The inconvenience that I have is when I deploy, since I always have an extra "+" sign, example: stable that the limit is 5.

  

1

     

1 + 1 +

     

1 + 1 + 1 +

     

1 + 1 + 1 + 1 +

     

1 + 1 + 1 + 1 + 1 +

and I get this answer. How could I do, to remove that extra sign so annoying? (I have tried in several ways, but I have not managed to reach the desired result)

code:

    String coma ="+";
    String serie="";
    int unos=1;
    int li=0;
    int cont=0;
    String u="1";
    Scanner sc = new Scanner(System.in);
    System.out.println("Ingrese limite para la generación de laa sucesión");
    li= sc.nextInt();
    int other;
    int other2;
    other=li;
    other2=li;
    other2=other2-1;
    int te=1;
    for(int i=1; i<=li; i++)
    {

        for(int j=0; j<(other-other2); j++)
        {
            if(i>1)
            {
                serie= u+coma;
                System.out.print(serie);

            }
            else
            {
                System.out.print(u);
            }

        }

        System.out.println("");
        other2--;



    }
    
asked by Axwell Duarte 18.11.2018 в 04:07
source

2 answers

0

Modifying the if with a composite condition to print serie to the limit minus 1.

if(i>1 && j<(other-other2)-1) {
  serie = u+coma;
  System.out.print(serie);
} else {
  System.out.print(u);
}

By the way, it is somewhat confusing about the variables other and other2

    
answered by 18.11.2018 в 05:09
0

Before the form prints the ones you should print the first one, and within the for you print only the missing ones by first prefixing the plus sign (+) . example for 5

public static void main(String[] args) {
  int limite = 5;
  for(int i = 0; i < limite; i++){
    System.out.print("1");
    for(int avance = 0; avance < i; avance++){
      System.out.print("+1");
    }
    System.out.println();
  }
}

The output of this program is

1
1+1
1+1+1
1+1+1+1
1+1+1+1+1
    
answered by 18.11.2018 в 04:29