Calculator in Java

Asked

Viewed 188 times

1

Everything was going well until the part where the result should appear.

public static void main( String[] args)
    {
        Scanner in = new Scanner(System.in);
        int num1,num2,resul;
         String sinal;
        System.out.println("Olá, entre com o primeiro numero: ");
        num1 = in.nextInt();
        System.out.println("Olá, entre com o segundo numero: ");
        num2 = in.nextInt();
        System.out.println("Soma (s) Menos (m) Vezes (v) Dividir (d): ");
        resul = in.nextInt();
        sinal = in.nextLine();

        if(sinal.equals ("s"))
        {
            resul = num1 + num2;
            System.out.println("Resultado final "+resul);
        }
        else if (sinal.equals ("m"))
        {
            resul = num1 - num2;
            System.out.println("Resultado final "+resul);
        }
        else if (sinal.equals ("v"))
        {
            resul = num1 * num2;
            System.out.println("Resultado final "+resul);
        }
        else if (sinal.equals("d"))
        {
            resul = num1 / num2;
            System.out.println("Resultado final "+resul);
        }
     }
  • I don’t know no haha, first time in java!

  • Luiz, you do not need to put "solved" in the title. Only the fact that you have accepted the answer already indicates that it is solved.

  • Ops haha, it was bad and thanks for the tip!

2 answers

2


One of the problems is this line:

resul = in.nextInt();

The value of resul will be calculated based on the values already read, so it makes no sense for you to read it from Scanner. You can remove this line.

Then the nextLine() called right after a nextInt() consumes the line break (the ENTER that the user typed after the number), and the signal will be empty (so it does not enter any of the if's after).

Instead, you can use next():

System.out.println("Olá, entre com o primeiro numero: ");
num1 = in.nextInt();
System.out.println("Olá, entre com o segundo numero: ");
num2 = in.nextInt();
System.out.println("Soma (s) Menos (m) Vezes (v) Dividir (d): ");
sinal = in.next();

Another detail is that when dividing two integers, the result will be rounded (7 / 2 gives 3; 2 / 5 gives zero, etc). If you want the most accurate result, with the decimal places, change the values to double:

double num1, num2, resul;
System.out.println("Olá, entre com o primeiro numero: ");
num1 = in.nextDouble();
System.out.println("Olá, entre com o segundo numero: ");
num2 = in.nextDouble();
System.out.println("Soma (s) Menos (m) Vezes (v) Dividir (d): ");
sinal = in.next();

0

I haven’t had the opportunity to test because of some factors, and I still can’t comment (so consider this as a comment) but I imagine the order in which you enter information here might be the problem. Since you may be confusing what is stored in the variables

System.out.println("Soma (s) Menos (m) Vezes (v) Dividir (d): ");
sinal = in.nextLine();      
resul = in.nextInt();
  • 1

    Opá, I tried here even so it was not (anyway thank you!!)

Browser other questions tagged

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