How to do so that the program tells me the sum of 5 is 10 and not the sum of 0 is 10

0

in the following code that compiles and executes I get that the number to do the sum is 0 but the sum to put the number that is if you do it correctly. Here the code:

import java.util.*;
class Args{
    public static void main (String[] args){
        //Atributos 
        int iNumero=0;
        int iSumatorio=0;
        int iCuenta= iNumero;
        System.out.println("Introduce un número para calcular su sumatoria");
        Scanner teclado = new Scanner(System.in);
        iCuenta = teclado.nextInt();
            while (iCuenta != 0) {
                iSumatorio = iSumatorio + iCuenta;
                iCuenta--;
                iSumatorio--;

            }

                System.out.println("El sumatorio de " + iCuenta + " es " + iSumatorio);
    }
}
    
asked by David Egea 09.04.2017 в 16:09
source

2 answers

0

Let's see ... in each iteration you are decreasing iCuenta in 1 ... with which, when leaving the while loop, iCuenta is worth 0 and it is correct to tell you "The sum of 0 is X". What you should do is to have another variable, to which you assign the original value of iCuenta (read from the keyboard) and use it to put the message, like this:

import java.util.*;
class Args{
    public static void main (String[] args){
        //Atributos 
        int iNumero=0;
        int iSumatorio=0;
        int iCuenta= iNumero;
        System.out.println("Introduce un número para calcular su sumatoria");
        Scanner teclado = new Scanner(System.in);
        iCuenta = teclado.nextInt();
        int iCuentaOrig = iCuenta;
            while (iCuenta != 0) {
                iSumatorio = iSumatorio + iCuenta;
                iCuenta--;
                iSumatorio--;

            }

                System.out.println("El sumatorio de " + iCuentaOrig + " es " + iSumatorio);
    }
}
    
answered by 09.04.2017 / 17:22
source
0

I see some failures.
First, the sum of 5 is 15, not 10 as you expect. (5 + 4 + 3 + 2 + 1 = 15)
Second, within while , after doing the sum ( iSumatorio=... ) decreases iSumatorio , which I do not see sense, and that is why it gives 10 and not 15 (when doing 5 iterations, decreases the result 5 times, that's why it gives 10).
Third, if you use Icuenta to count the iteriaciones, decreasing ( iCuenta--; ), at the end of the loop it will evidently be worth 0 (in fact it is worth 0 is the output condition of the loop) and therefore when the variable shows 0 and no 5.
To finish you have declared a variable that you do not use ( iNumero ).

A possible solution:
If you do not want to modify the variable where you keep the number to do the summation, to use it later, use another to act as a counter, for example, save in " iNumero " the number entered on the keyboard, and use iCuenta as a counter .
As the number of iterations is fixed, it may be more understandable to use a for loop and not a while .
Sample code:

import java.util.*;
class Args{
    public static void main (String[] args){
        //Atributos 
        int iNumero, iCuenta;
        int iSumatorio=0;

        System.out.println("Introduce un número para calcular su sumatoria");
        Scanner teclado = new Scanner(System.in);
        iNumero= teclado.nextInt();

        for (iCuenta=1 ; iCuenta<=iNumero ; iCuenta++) {
            iSumatorio += iCuenta;
        }

        System.out.println("El sumatorio de " + iNumero+ " es " + iSumatorio);
    }
}
    
answered by 09.04.2017 в 17:21