Una forma sencilla y rápida de aprender JAVA, observando y deduciendo cómo se comporta el lenguaje a través de ejemplos prácticos.
Archivo del blog
martes, 11 de febrero de 2014
Operaciones matemáticas. Sumatorio.
Codigo:
//Sumatorio
package sumatorio;
import java.util.Scanner;
public class Sumatorio {
public static void main(String[] args) {
//Inicializar variables
int[] x = new int[5];
for (int i = 1; i < x.length; i++) {
System.out.println("Introduce valor X" + i + ":");
Scanner leer = new Scanner(System.in);
x[i] = Integer.parseInt(leer.next());
}
int n = x.length;
int m = 1;
int sumatorio = 0;
String formula = "";
//Sumatorio
for (int i = m; i < n; i++) {
sumatorio += x[i];
formula += " + " + x[i];
}
System.out.println("\n∑ =" + formula.substring(2) + " = " + sumatorio);
}
}
Resultado:
run:
Introduce valor X1:
23
Introduce valor X2:
61
Introduce valor X3:
7
Introduce valor X4:
99
∑ = 23 + 61 + 7 + 99 = 190
BUILD SUCCESSFUL (total time: 11 seconds)
Suscribirse a:
Entradas (Atom)
Con la tecnología de Blogger.