What’s wrong with it?

Asked

Viewed 61 times

-4

import java.util.Scanner;

public class desafio {
    public static void main(String args[]) {
        char op;
        float soma;
        float mult;
        float sub;
        float div;
        float num;

        Scanner entrada = new Scanner(System.in);
        op = entrada.next().charAt(0);

            switch (op) {
                case '+': while (num = entrada.nextFloat() != 0) {
                    soma += num;
                }
                    break;

                case '-': while (num = entrada.nextFloat() != 0) {
                    sub = entrada;
                    sub -= num;
                }
                    break;
                case 'x': while(num = entrada.nextFloat() != 0) {
                    mult = 1;
                    mult *= num;
                }
                case '/': while(num = entrada.nextFloat() != 0) {
                    div = 1;
                    num = entrada;
                }
            }
    }
}
  • You’re splitting right?

  • div = 1?; you are sure of this?

  • Actually I sent it before changing the last operation however some strange errors appear. (I’m starting in java). "incompatible types".

  • First you need to say what you want to do. Even so: you did not initialize the sum variable before using it, in sum += num. In your division you are just doing: num = input. Maybe that’s not what you want.

1 answer

0


When you declare num = entrada.nextFloat() != 0 this way you end up assigning a value of type boolean for num, this because the expression in this way returns whether or not the number typed is equal to zero, ie a true or false. As you previously stated that no one is a float you get the error of imcompatible types.

With a simple modified your code works:

import java.util.Scanner;

public class Desafio {

    public static void main(String args[]) {
        char op;
        float result = 0;
        float num;

        Scanner entrada = new Scanner(System.in);
        op = entrada.next().charAt(0);

        switch (op) {
            case '+':
                System.out.println("Você escolheu soma!");
                do {
                    System.out.println("Digite um número para somar: (Digite 0 para sair)");
                    num = entrada.nextFloat();
                    result += num;
                } while (num != 0);
                break;

            case '-':
                System.out.println("Você escolheu subtração!");
                do {
                    System.out.println("Digite um número para subtrair: (Digite 0 para sair)");
                    num = entrada.nextFloat();
                    result -= num;
                } while (num != 0);
                break;

            case 'x':
                System.out.println("Você escolheu multiplicação!");
                do {
                    System.out.println("Digite um número para multiplicar: (Digite 0 para sair)");
                    num = entrada.nextFloat();
                    result *= num;
                } while (num != 0);
                break;

            case '/':
                System.out.println("Você escolheu divisão!");
                do {
                    System.out.println("Digite um número para dividir: (Digite 0 para sair)");
                    num = entrada.nextFloat();
                    result /= num;
                } while (num != 0);
                break;
            default:
                System.out.println("Nenhuma operação matemática escolhida.");
        }

        System.out.println(result);
    }

}

Two remarks: Always set the default in the switch case to treat if no condition is met and remember that class name always with the first letter uppercase ;)

Otherwise that’s it, good studies!

  • Bruno, thanks a lot for the help, managed to clarify this doubt I had about the error generated. Thanks for the tips, I will pay more attention!

  • @Lucasaranha if this answer solved as a problem, mark it as a solution. It is the "check" that appears under the number of votes.

Browser other questions tagged

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