Code with error in Switch Case

-1

I have to do the following exercise:

  

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

     

1) Option: the 10% discount view

     

2) Option: twice (price tag)

     

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

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

The error 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;

        }

    }

}
    
asked by anonymous 03.10.2017 / 00:07

1 answer

1

There are some errors to fix.

  

First

The main method is a static method. In order to access some variable or method they must also be static.

To do this, change your methods to be static

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

Second

switch is getting a variable that does not exist, to fix it can do two ways.

  • Call the method op direct.

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

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

    You can see it working at repl.it

         

    Note: I am no professional, there may be other ways to solve.

        
    03.10.2017 / 00:45