Sum of component numbers

Asked

Viewed 446 times

2

I’m trying to solve the following problem using DESVIO CONDICIONAL COMPOSTO. I can’t imagine how I calculate the numbers that make up the digit

Request an integer value from the user between 100 and 999. If the read value is less than 500, display the sum between its component numbers. Otherwise, subtract its components.

I only know I must wear IF and ELSE, where I will place this condition

if(nb > 500 ) {

for the sum of the component numbers to occur.

What I broke my head but really couldn’t come to a conclusion is how I’m going to accomplish the sum!

For example, if the number is 328, how will I add the numbers 3, 2 and 8?

3 answers

2


  1. Read the user number:

    Scanner entrada = new Scanner(System.in);
    int numero = Integer.parseInt(entrada.nextLine());
    
  2. Make sure the number is ok:

    if (numero < 100 || numero > 999) {
        System.out.println("Esse número não serve.");
        return;
    }
    
  3. Separate the components:

    int centenas = numero / 100;
    int dezenas = numero / 10 % 10;
    int unidades = numero % 10;
    

From now on, I’m sure you already know how to do it. :)

0

Use the split to transform the value into an array of values and loop to read and add

  String[] array = numeroQueOUsuarioDigitar.split("");

  int total = 0;

  if(Integer.parseInt(numeroQueOUsuarioDigitar) < 500) {
      for(String e : array) {
          total += Integer.parseInt(e);
      }
  } else {
      for(String e : array) {
          total -= Integer.parseInt(e);
      }
 }
  • Ours is even more confusing now!

  • Exercise is actually only with If and Else..

  • How so just with if and Else? What you didn’t understand?

  • You gave me a Solution with For, but for this exercise I should only use If(condition) and Else{ instruction I should only use :IF a condition is TRUE, THEN deviate and perform the instructions linked to the true condition ELSE if that condition is FALSE, deviates and performs the instructions linked to the false condition

  • From what I understood the question the doubt was how to sum the digits of a number and that’s what the code does, now just put the if to the loop add and Else to the reverse. I got the question wrong?

  • Exactly William! If the number for example is 328 ,I must add the three numbers to get the result!

  • Then, that’s what the code does, the split splits the string to generate an array with 3 elements, then a loop is made to add each item of the array to the total variable. Have you tested?

  • And that would work for any number? Because the user will enter the number,!

  • Yes, but if the variable value is not a number it will generate an exception when trying to convert

  • I’ll try then! Until tomorrow I’ll come here and tell you if it worked!

  • I edited the answer with a more complete example

Show 6 more comments

0

Breaks the value in substrings and goes back to int.

    if(nb > 500 ) {
        String aux = Integer.toString(i);
        //TRANSFORMA O NUMERO EM STRING
        int un = Integer.parseInt(aux.substring(0, 1));
        //PEGA O PRIMEIRO CRACTER E TRANSFORMA EM INT
        int de = Integer.parseInt(aux.substring(1, 2));
        //PEGA O SEGUNDO CRACTER E TRANSFORMA EM INT
        int ce = Integer.parseInt(aux.substring(2, 3));
        //PEGA O TERCEIRO CRACTER E TRANSFORMA EM INT
        int total = ce + de + un;
        //SOMA OS COMPONENTES
    }else{
        String aux = Integer.toString(i);
        //TRANSFORMA O NUMERO EM STRING
        int un = Integer.parseInt(aux.substring(0, 1));
        //PEGA O PRIMEIRO CRACTER E TRANSFORMA EM INT
        int de = Integer.parseInt(aux.substring(1, 2));
        //PEGA O SEGUNDO CRACTER E TRANSFORMA EM INT
        int ce = Integer.parseInt(aux.substring(2, 3));
        //PEGA O TERCEIRO CRACTER E TRANSFORMA EM INT
        int total = ce - de - un;
        //SUBTRAI OS COMPONENTES
    }

It is a very beginner code, but so you do using only if and Else

  • Work with Strings to solve mathematical problems is not a good idea.

  • But it passes to int before making the sum, so it makes no difference, the string is only to break what was typed into 3 different numbers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.