Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ejercicio4;
- import java.util.Scanner;
- /**
- *
- * @author 988699
- */
- public class Ejercicio4 {
- static int i = 1;
- /**
- * @param args the command line arguments
- */
- public static int sum(int n1, int n2) {
- return n1+n2;
- }
- public static int sum(int n1, int n2, int n3) {
- return n1+n2+n3;
- }
- public static int restar(int n1, int n2) {
- int res = 0;
- if(n2 >= n1) {
- res = n2-n1;
- }else if(n1 > n2) {
- res = n1-n2;
- }
- return res;
- }
- public static int moltiplication(int n1, int n2) {
- return n1*n2;
- }
- public static double divide(int n1, int n2) {
- int res = 0;
- if(n2 >= n1) {
- res = n2/n1;
- }else if(n1 > n2) {
- res = n1/n2;
- }
- return res;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int op;
- int n1;
- int n2;
- int n3;
- int op2;
- while(i == 1) {
- System.out.print("1 - Sumar\n");
- System.out.print("2 - Restar\n");
- System.out.print("3 - Moltiplicar\n");
- System.out.print("4 - Dividir\n");
- System.out.print("0 - Salir\n");
- System.out.print("Press a button to continue: ");
- op = sc.nextInt();
- switch(op) {
- case 1:
- System.out.println("2 - to sum 2 number\n");
- System.out.println("3 - to sum 3 number\n");
- op2 = sc.nextInt();
- if(op2 == 2) {
- System.out.print("- SUM2 -\n");
- System.out.print("Primo numero: ");
- n1 = sc.nextInt();
- System.out.print("Segundo numero: ");
- n2 = sc.nextInt();
- System.out.printf("Resultado = %d\n", sum(n1,n2));
- }else if (op2 == 3) {
- System.out.print("- SUM3 -\n");
- System.out.print("Primo numero: ");
- n1 = sc.nextInt();
- System.out.print("Segundo numero: ");
- n2 = sc.nextInt();
- System.out.print("Terzero numero: ");
- n3 = sc.nextInt();
- System.out.printf("Resultado = %d\n", sum(n1,n2,n3));
- }else {
- System.out.print("Error wrong option\n");
- }
- break;
- case 2:
- System.out.print("- RESTAR -\n");
- System.out.print("Primero numero: ");
- n1 = sc.nextInt();
- System.out.print("Segundo numero: ");
- n2 = sc.nextInt();
- System.out.printf("Resultado = %d\n", restar(n1,n2));
- break;
- case 3:
- System.out.print("- MOLTIPLICATION -\n");
- System.out.print("Primero numero: ");
- n1 = sc.nextInt();
- System.out.print("Segundo numero: ");
- n2 = sc.nextInt();
- System.out.printf("Resultado = %d\n", moltiplication(n1,n2));
- break;
- case 4:
- System.out.print("- DIVIDE -\n");
- System.out.print("Primero numero: ");
- n1 = sc.nextInt();
- System.out.print("Segundo numero: ");
- n2 = sc.nextInt();
- System.out.printf("Resultado = %.2f\n", divide(n1,n2));
- break;
- case 0:
- System.out.print("Adios.");
- i = 0;
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement