Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
Cómo sumar dos enteros en una línea

Hola, soy nuevo en Java y en mi clase, mi profesor quiere que creemos un programa que calcule la suma de dos números. (Utilice métodos) Por ejemplo: Ingrese un número entero: 95 //Salida esperada: La suma es 14 //(9+5=14)

Realmente estoy perdido en esto porque realmente no sé cómo hacer esto, por favor ayuda.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Desde el punto de vista del algoritmo, la forma más fácil sería:

  • convertir el entero a una cadena
  • iterar los personajes
  • convertir cada carácter de nuevo a un int
  • hacer la suma

En Java "Moderno":

 String.valueOf(test).chars().mapToLong(c -> Long.parseLong("" + (char) c)).reduce(Long::sum).getAsLong()

En Java más antiguo:

 int test = 95; long sum = 0; for (char c : String.valueOf(test).toCharArray()) { sum += Integer.parseInt(String.valueOf(c)); } System.out.println(sum);

En javascript:

 let sum = 0 for (let c of String(test)) { sum += parseInt(c) } console.log(sum)
about 4 years ago · Juan Pablo Isaza Report

0

Si usa Java, puede usar este código:

 int num = 95; int sum = 0; while (0 != num) { // add the last digit of the given number to the sum sum = sum + (num % 10); // remove the last digit of the given number num = num / 10; } System.out.println(sum);

(El código en JavaScript es muy similar)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!