Sie sind auf Seite 1von 4

1

Lección 3 Doc. I. Prado

La sentencia switch

Evalúa una variable var y de acuerdo al valor de esta ejecuta el caso. Su formato
es:

switch( var ) {
case expr2:
sentencias;
break;
case expr3:
sentencias;
break;
default:
sentencias;
break;
}

Ejemplo 10. El siguiente programa presenta un menú de opciones para sumar,


restar, multiplicar o dividir dos números que ingresan por teclado.
public class pow
{
public static void main (String args []) throws IOException
{
BufferedReader in;
in = new BufferedReader (new InputStreamReader (System.in));
int a, b, c;
float k,e,d;
System.out.println ("**** OPERACIONES ARITMETICAS \n");
System.out.println ("1.Suma. \n2.Resta \3.Multiplicacion \n4.Division \n5.Salir");
System.out.println ("Su opcion?");
int n = Integer.parseInt (in.readLine ());
switch (n)
{
case 1:
System.out.println ("**** SUMA ****");
System.out.println ("Primer dato?");
a = Integer.parseInt (in.readLine ());
System.out.println ("Primer dato?");
b = Integer.parseInt (in.readLine ());
c = a + b;
System.out.println ("La suma es " + c);
break;
2

Lección 3 Doc. I. Prado


case 2:
System.out.println ("**** RESTA ****");
System.out.println ("Primer dato?");
a = Integer.parseInt (in.readLine ());
System.out.println ("Primer dato?");
b = Integer.parseInt (in.readLine ());
c = a - b;
System.out.println ("La suma es " + c);
break;
case 3:
System.out.println ("**** MULTIPLICACION ****");
System.out.println ("Primer dato?");
a = Integer.parseInt (in.readLine ());
System.out.println ("Primer dato?");
b = Integer.parseInt (in.readLine ());
c = a * b;
System.out.println ("La suma es " + c);
break;
case 4:
System.out.println ("**** DIVISION ****");
System.out.println ("Numerador?");
a = Integer.parseInt (in.readLine ());
System.out.println ("Denominador?");
b = Integer.parseInt (in.readLine ());
if (b != 0)
{
e=a; // lo convertimos a real
d=b; // lo convertimos a real
k=e/d;
System.out.println ("El cociente es: " + k);
}
else
System.out.println ("Error! division por cero ");
break;
default:
System.out.println (" Sin operaciones que realizar! ");
break;
}
}
}

Ejemplo 11.- Otra forma de convertir enteros a reales. El siguiente ejemplo


divide dos números que inicialmente ingresan como enteros, para no perder
decimales del resultado de la división se los convierte a reales.
3

Lección 3 Doc. I. Prado

import java.io.*;
public class tipos_datos
{
//ejemplo conversion de tipos de datos en java
public static void main(String arg[]) throws IOException
{
BufferedReader in;
in = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("Numerador?");
int a = Integer.parseInt (in.readLine ());
System.out.println ("Denominador?");
int b = Integer.parseInt (in.readLine ());
float c;
c=(float) a/b;
System.out.println(c);
}
}

Ejemplo 12.- Utilización de la clase Math y la función pow para calcular a


elevado a la b.
import java.io.*;
public class pow
{
public static void main(String args[]) throws IOException
{
BufferedReader in;
in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Programa para calcular a elevado a la b");
System.out.println("Ingrese la base: ");
float a=(Float.valueOf (in.readLine ())).floatValue ();
System.out.println("Ingrese el exponente: ");
float b=(Float.valueOf (in.readLine ())).floatValue ();
double r=Math.pow(a,b);
System.out.println("El resultado es: "+r);
}
}

Ejemplo 13.- Este ejemplo utiliza la clase Math para poder aplicar diferentes
funciones trigonométricas (matemáticas) como ser: seno, coseno, PI
4

Lección 3 Doc. I. Prado


import java.io.*;
import java.lang.Math;
public class matematicas
{
public static void main(String args[]) throws IOException
{
BufferedReader in;
in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Escriba el angulo en radianes");
double x = (Double.valueOf(in.readLine())).doubleValue();
double y = Math.cos(x);
System.out.println("El coseno es: "+y);
y = Math.sin(x);
System.out.println("El seno es: "+y);
System.out.println("El valor de PI es:"+Math.PI);
}
}

Ejemplo 14.- Generando números randómicos.


import java.io.*;
import java.lang.Math;
public class random1
{
public static void main(String args[]) throws IOException
{
double r;
int k;
r = Math.random();
System.out.println("Un numero randomico real normal "+r);
k=(int)(r*10);
System.out.println("El numero randomico ahora entero "+k);
k = (int)(Math.random()*10); //obtengo otro entero
System.out.println("Otro numero randomico entero "+k);
}
}

Visite el sitio: http://www.cica.es/formacion/JavaTut/Cap3/math.html para


encontrar más funciones matemáticas.

Das könnte Ihnen auch gefallen