Do While goes into infinite loop

2

I'm sure my error is in while , but I can not understand what I did wrong. It enters loop infinity.

The statement follows:

  

Implement a program that receives 3 arguments from the command line. O   first and second argument are real numbers and the third argument is   operation.

Follow the code:

import java.io.*;
import java.net.*;
import java.util.Scanner;

class Argumentos {
    public static void main(String[] args){
        float resultado = 0;

        System.out.println("Insira um valor: ");
        Scanner scanner1 = new Scanner(System.in);
        float num1 = scanner1.nextFloat();

        System.out.println("Insira outro valor: ");
        Scanner scanner2 = new Scanner(System.in);
        float num2 = scanner2.nextFloat();

        System.out.println("Insira a operação (+, -, / ou *): ");


        System.out.println("Insira a operação (+, -, / ou *): ");
        Scanner c = new Scanner(System.in);
        char operacao = c.next().charAt(0);

        do {

            if(operacao == '*') {
                resultado = (num1*num2);
            } else if (operacao == '/') {
                resultado = (num1/num2);
            } else if (operacao == '+') {
                resultado = (num1+num2);
            } else if (operacao == '-') {
                resultado = (num1-num2);
            }

            System.out.println("Insira a operação (+, -, / ou *): ");
            operacao = c.next().charAt(0);

            System.out.println("Resultado da conta: " + resultado);

        } while((operacao != '*') || (operacao != '/') || (operacao != '-') || (operacao != '+'));

        System.out.println("Resultado da conta: " + resultado);

    }
}
    
asked by anonymous 30.08.2016 / 01:13

2 answers

6

It is infinite because this expression will always be true:

while((operacao != '*') || (operacao != '/') || (operacao != '-') || (operacao != '+'));

Let's get the first comparison:

operacao != '*'

Consider that operacao is * . For being equal the result would be false , since you are asking if it is different.

So let's look at the following comparison?

operacao != '/'

We already know that operacao is * , so there is only one result chance there and it is true , * is different from / .

It is not necessary to evaluate the other comparisons because you already have a final result for every expression, after all when using || there is a short circuit because just one of the OR operands is true, that the whole will be true.

You probably want this:

while (operacao != '*' && operacao != '/' && operacao != '-' && operacao != '+');

So just continue if all four comparisons are true, that is, if none of these characters were typed.

All this can be observed in the truth table .

It would look something like this:

import java.io.*;
import java.net.*;
import java.util.Scanner;

class Argumentos {
    public static void main(String[] args) {
        float resultado = 0;
        System.out.println("Insira um valor: ");
        Scanner scanner1 = new Scanner(System.in);
        float num1 = scanner1.nextFloat();
        System.out.println("Insira outro valor: ");
        Scanner scanner2 = new Scanner(System.in);
        float num2 = scanner2.nextFloat();
        System.out.println("Insira a operação (+, -, / ou *): ");
        Scanner c = new Scanner(System.in);
        char operacao = c.next().charAt(0);
        do {
            if (operacao == '*') {
                resultado = num1 * num2;
            } else if (operacao == '/') {
                resultado = num1 / num2;
            } else if (operacao == '+') {
                resultado = num1 + num2;
            } else if (operacao == '-') {
                resultado = num1 - num2;
            }
            System.out.println("Insira a operação (+, -, / ou *): ");
            operacao = c.next().charAt(0);
            System.out.println("Resultado da conta: " + resultado);
        } while (operacao != '*' && operacao != '/' && operacao != '-' && operacao != '+');
        System.out.println("Resultado da conta: " + resultado);
    }
}

It even gives a little simplification.

    
30.08.2016 / 01:21
1

If I understand correctly, your goal is to perform operations while the user enters an operation code, be it '+' , '-' , '/' or '*' .

If it is, your do while is reversed. The correct is something like: "Do this, as long as what is typed equals this."

You are doing: "Do this, as long as what is typed is different."

So your code would look like this

//...
do {
   if(operacao == '*') {
      resultado = (num1*num2);
   } else if (operacao == '/') {
      resultado = (num1/num2);
   } else if (operacao == '+') {
      resultado = (num1+num2);
   } else if (operacao == '-') {
      resultado = (num1-num2);
   }
   System.out.println("Insira a operação (+, -, / ou *): ");
   operacao = c.next().charAt(0);
   System.out.println("Resultado da conta: " + resultado);
} while((operacao == '*') || (operacao == '/') || (operacao == '-') || (operacao == '+'));
//..
    
30.08.2016 / 01:21