I cannot perform the indicated operations

Asked

Viewed 105 times

-2

Prepare a program that, given 2 integer vectors of 20 positions, performs the respective operations indicated by a third vector of characters of 20 positions also provided by the user, containing the four arithmetic operations in any combination, storing the results in a fourth vector.

public class Funcao {

    int v1[] = new int [20];
    int v2[] = new int [20];
    String oper [] = new String[20];
    double result [] = new double [20];


int i;
for (i=0;i<20;i++){
v1[i] = Integer.parseInt(JOptionPane.showInputDialog("Digite "+(i+1)+" operando"));
oper[i] = JOptionPane.showInputDialog("Digite "+(i+1)+" operador(+,-,*,/)");
v2[i] = Integer.parseInt(JOptionPane.showInputDialog("Digite "+(i+1)+" operando"));
        }

    }

1 answer

-1


for(int i=0; i<20; i++){
        switch(oper[i]){
            case "+":
                result[i] = v1[i] + v2[i];
                break;
            case "-":
                result[i] = v1[i] - v2[i];
                break;
            case "*":
                result[i] = v1[i] * v2[i];
                break;
            case "/":
                result[i] = v1[i] / v2[i];
                break;
            default:
                System.out.println("Operador inválido");
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.