Do While enters infinite loop

Asked

Viewed 249 times

3

I’m sure my mistake is in while, but I can’t understand what I did wrong. He goes into loop infinite.

Follows the statement:

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

Follows 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);

    }
}

2 answers

8


It’s infinite because that expression will always be true:

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

Let’s take the first comparison:

operacao != '*'

Let us consider that operacao worth *. For being equal the result would be false, since you’re asking if it’s any different.

Here we look at the following comparison?

operacao != '/'

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

Nor need to evaluate the other comparisons because there is already a final result for every expression, after all when using || there is a Circuit short because one of the operands of the "OR" is true, that the whole will be true.

You probably wish that:

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

So only continue if all four comparisons are true, ie if none of these characters were typed.

All this can be observed in the truth table.

That would be about it:

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);
    }
}

I put in the Github for future reference.

You can even simplify a little.

1

If I understand correctly your objective is to execute the operations while the user enters a code of the operation, be it '+', '-', '/' or '*'.

If that’s it, you do while is reversed. Right is something like: "Do this, as long as what you type is equal to that."

You’re making: "Do this, while what you type is different from what."

Soon 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 == '+'));
//..

Browser other questions tagged

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