Saltar al contenido

cómo hacer un ejemplo de código de calculadora java simple

Nuestro grupo de especialistas pasados algunos días de investigación y de recopilar de datos, hallamos los datos necesarios, nuestro deseo es que te sea de gran utilidad para tu trabajo.

Ejemplo 1: como hacer una calculadora en java

// this was all coded by me, to use it get Eclipse, or IntelliJ and make a new// Java ProjectpackagejavaCode;importjava.util.Scanner;publicclass testCode publicstaticvoidmain(String[] args)Scanner scan =newScanner(System.in);System.out.println("Would you like to Add, Subtract, Divide, or Multiply?");String mathType = scan.next();if(mathType.equalsIgnoreCase("add"))System.out.println("Whats the first number you want to add?");int firstNumber = scan.nextInt();System.out.println(firstNumber +" + ");System.out.println("Enter your second number");int secondNumber = scan.nextInt();System.out.println(firstNumber +" + "+ secondNumber +" =");System.out.println(firstNumber + secondNumber);elseif(mathType.equalsIgnoreCase("subtract"))System.out.println("Whats the first number you want to subtract?");int firstNumber = scan.nextInt();System.out.println(firstNumber +" - ");System.out.println("Enter your second number");int secondNumber = scan.nextInt();System.out.println(firstNumber +" - "+ secondNumber +" =");System.out.println(firstNumber - secondNumber);elseif(mathType.equalsIgnoreCase("divide"))System.out.println("Whats the first number you want to divide?");int firstNumber = scan.nextInt();System.out.println(firstNumber +" % ");System.out.println("Enter your second number");int secondNumber = scan.nextInt();System.out.println(firstNumber +" % "+ secondNumber +" =");System.out.println(firstNumber / secondNumber);elseif(mathType.equalsIgnoreCase("multiply"))System.out.println("Whats the first number you want to multiply?");int firstNumber = scan.nextInt();System.out.println(firstNumber +" x ");System.out.println("Enter your second number");int secondNumber = scan.nextInt();System.out.println(firstNumber +" x "+ secondNumber +" =");System.out.println(firstNumber * secondNumber);

Ejemplo 2: programa de calculadora simple en java

importjava.util.Scanner;publicclassCalculatorpublicstaticvoidmain(String[] args)Scanner reader =newScanner(System.in);System.out.print("Enter two numbers: ");// nextDouble() reads the next double from the keyboarddouble first = reader.nextDouble();double second = reader.nextDouble();System.out.print("Enter an operator (+, -, *, /): ");char operator = reader.next().charAt(0);double result;switch(operator)case'+':
                result = first + second;break;case'-':
                result = first - second;break;case'*':
                result = first * second;break;case'/':
                result = first / second;break;// operator doesn't match any case constant (+, -, *, /)default:System.out.printf("Error! operator is not correct");return;System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);

Si guardas alguna desconfianza y disposición de progresar nuestro crónica puedes realizar una interpretación y con placer lo estudiaremos.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *