Method that returns False and to While True

Asked

Viewed 46 times

-2

public boolean para () {
    return false;
}

public void acao (int acao) {
    if (acao == 1) {
        System.out.println("Diga quanto quer depositar: ");
        Scanner sc = new Scanner (System.in);
        double valor = sc.nextDouble();
        this.saldo += valor;
        
    }
    if (acao == 2) {
        System.out.println("Diga quanto quer depositar: ");
        Scanner sc = new Scanner (System.in);
        double valor = sc.nextDouble();
        if (valor > this.saldo) {
            System.out.println("Você não tem esse valor");
        }else {
            this.saldo -= valor;
        }
    }
    
    if (acao == 3) {
        System.out.println("Seu saldo é: " + this.saldo);
    }
    
    else {
        this.para();
    }
}

I created this method to choose which function the program will do, when other than 1,2,3 I wanted to return false to make While (true) stop.

while (true){
        System.out.println("Escolha uma função 1- Depositar  2- Sacar  3- Ver Saldo");
        int acao = sc.nextInt();
        
        usuario01.acao(acao);
        
    }

Where is the error and pq does not return false?

  • When you do this.para, is calling the method, but its return is "lost" (it is not assigned to any variable, nor used anywhere). Anyway, this method (which only returns false) is half useless. To work, an alternative is the method acao return the boolean whether to continue or not: https://ideone.com/qLnJZW

  • got it. very obg

1 answer

-2

There are some basic errors of interpretation in your code, in case you need to have a stop control. follow the example.

import java.util.Scanner;

public class AcoesParada {
    Double saldo = 0D;
    static boolean b = true;

    public static void para() {
        AcoesParada.b = !AcoesParada.b;
    }

    public void acao(int acao) {
        if (acao == 1) {
            System.out.println("Diga quanto quer depositar: ");
            Scanner sc = new Scanner (System.in);
            double valor = sc.nextDouble();
            this.saldo += valor;
            acao = 0;
        }
        if (acao == 2) {
            System.out.println("Diga quanto quer sacar: ");
            Scanner sc = new Scanner (System.in);
            double valor = sc.nextDouble();
            if (valor > this.saldo) {
                System.out.println("Você não tem esse valor");
            }else {
                this.saldo -= valor;
            }
            acao = 0;
        }
        if (acao == 3) {
            System.out.println("Seu saldo é: " + this.saldo);
            acao = 0;
        }
        if(acao == 4) {
            AcoesParada.para();
        }
    }

    public static void main(String[] args) {
        AcoesParada usuario01  = new AcoesParada();
        Scanner sc = new Scanner (System.in);
        while(AcoesParada.b){
            System.out.println("Escolha uma função 1- Depositar  2- Sacar  3- Ver Saldo 4-Sair");
            int acao = sc.nextInt();
            usuario01.acao(acao);
        }
    }
}

Browser other questions tagged

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