Error code in Switch Case

Asked

Viewed 394 times

-1

I have to do the following exercise:

Make a program in Java that requests the total amount spent by the a store, print the payment options, request the desired option and prints the total amount of installments (if any).

1) Option: the view with 10% discount

2) Option: twice (label price)

3) Option: 3 to 10 times with 3% interest per month (only for purchases above R$ 100,00). Use the Switch command.

I’m having trouble finding where I’m going wrong in the code below:

The mistake is when I call the switch.

package Prova_A;

import java.util.Scanner;

public class Exe_02 {

    Scanner ler = new Scanner(System.in);

    int op() {

        Scanner ler = new Scanner(System.in);
        int op;

        System.out.println("*****Modo de Pagamento*****");
        System.out.println("1 - Á Vista");
        System.out.println("2 - 2 Vezes");
        System.out.println("3 - 3 Vezes");

        op = ler.nextInt();
        System.out.println("*******************************\n");
        return (op);
    }

    float aVista(float vlr) {
        System.out.println("Total R$: " + vlr);
        System.out.println("Total a pagar R$: " + vlr * 0.9);

        return (0);
    }

    float duasVezes(float vlr) {
        System.out.println("Total R$: " + vlr);
        System.out.println("Total a pagar R$: " + vlr / 2);

        return (0);
    }

    float tresVezes(float vlr) {

        int parcelas;

        System.out.println("Total R$: " + vlr);

        do {

            System.out.println("Informe a quantidade de parcelas :\n");
            parcelas = ler.nextInt();

        } while ((parcelas > 10) || (parcelas < 3));

        System.out.println("Parcelas de R$:\n " + parcelas + (vlr * 1.03) / parcelas);

        return (0);

    }

    public static void main(String[] args) {

        Scanner ler1 = new Scanner(System.in);

        float vlrCompra;
        int opcao;

        System.out.println("Informe o total da compra: \n");
        vlrCompra = ler1.nextFloat();

        System.out.println("***************************\n");

        switch(op){

            case 1:
                System.out.println("Pagamento a vista: \n");
                vlrCompra = ler.nextFloat();
                break;

        }

    }

}
  • I’m not seeing where you read the "op" on your switch

  • Gustavo, I’m trying to read here int op() { at the beginning of the code

1 answer

1


There are some mistakes to be corrected.

First

The method main is static method, in order to be able to access some variable or method they must also be static.

For this change your methods to be static

static int op()
// ... e assim por diante.

According to

The switch is receiving a variable that does not exist, to correct can do two ways.

  1. Call the method op straightforward.

    switch(op()) //..
    
  2. Create the variable op

    int op = op();
    switch(op) //..
    

You can see it working on repl it.

Obs: I’m no professional, there may be other ways to solve.

  • Wéllingthon, thank you very much was that... I’m still learning this java syntax...

Browser other questions tagged

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