codigo en java

Download Codigo en Java

If you can't read please download the document

Upload: krliithoz-piiyuyiim

Post on 05-Sep-2015

174 views

Category:

Documents


5 download

DESCRIPTION

Codigo

TRANSCRIPT

package trabajo;import java.util.Scanner;public class Trabajo { public static long hexa (String n) { long suma = 0; StringBuilder num = new StringBuilder (n). reverse(); for(int i = 0; i < num.length(); i++) { if (num.charAt(i)== 'A') { suma += 10*(Math.pow(16, i)); } else { if(num.charAt(i)=='B') { suma += 11*(Math.pow(16, i)); } else { if (num.charAt(i)== 'C') { suma += 12*(Math.pow(16, i)); } else { if (num.charAt(i)== 'D') { suma += 13*(Math.pow(16, i)); } else { if (num.charAt(i)== 'E') { suma += 14*(Math.pow(16, i)); } else { if (num.charAt(i)== 'F') { suma += 15*(Math.pow(16, i)); } else { suma = suma + Integer.parseInt(""+num.charAt(i))*(long)Math.pow(16, i); } } } } } } } return suma; } public static void main(String[] args) { long numd1, numd2; int resultado=0, operaciones; Scanner hexadecimal = new Scanner (System.in); System.out.print("Ingrese porfavor el primer numero hexadecimal: "); String numero1 = hexadecimal.nextLine(). toUpperCase(); System.out.print("Ingrese porfavor el segundo numero hexadecimal: "); String numero2 = hexadecimal.nextLine(). toUpperCase(); numd1=hexa(numero1); numd2=hexa(numero2); System.out.println("operaciones 1. suma 2. Resta 3.Multiplicacion 4.Division 5.Salir"); operaciones = hexadecimal.nextInt(); while (operaciones!=5) { switch (operaciones) { case 1: resultado=(int) (numd1+numd2); break; case 2: resultado=(int) (numd1-numd2); break; case 3: resultado=(int) (numd1*numd2); break; case 4: if (numd2==0) { resultado=99999; } else { resultado=(int) (numd1/numd2); } break; } // condicion: para los numeros negativos if (resultado < 0) { System.out.println("No existen los numeros hexadecimales negativos"); } else { // condicion: la division para cero no esta permitida if (resultado ==99999) { System.out.println("La division para cero no existe"); } else { // proceso para transforma de decimal a hexadecimal String str = Integer.toHexString(resultado); System.out.println("El resultado de la operacion " +operaciones+ " \t es: \n" +str); } } System.out.println("operaciones 1. suma 2. Resta 3.Multiplicacion 4.Division 5.Salir"); operaciones = hexadecimal.nextInt(); } }}