Why is this algorithm in Java not running?

Asked

Viewed 238 times

-1

import java.util.Scanner;

public class CalculoSalarioLiquido {

    public static void main (String args []) {

        System.out.println("Programa para calcular o Salário Líquido: ");

        System.out.println("Escreva o Número de Horas Trabalhadas pelo Funcionário: ");
        Scanner sc = new Scanner (System.in);
        double numerodehorastrabalhadas = sc.nextInt();

        System.out.println("Escreva o Número de Horas Extras Trabalhadas pelo Funcionário: ");
        double numerodehorasextras = sc.nextInt();

    **//Tem vários erros nestes quatro System.out.println. Estou com problemas na importação dos métodos, para depois imprimir os resultados. Erros de not a statement.** 
        System.out.println ("Salário Bruto: " (calcularsalariodofuncionario(numeroDeHorasTrabalhadas,numeroDeHorasExtras)));
        System.out.println ("Imposto de Renda: "calcularImpostodeRenda(salariobruto));
        System.out.println ("Seguro Social: " calcularSeguridadeSocial(salariobruto));
        System.out.println ("Salário Líquido: " calcularSalarioLiquido(salariobruto, seguridadesocial, impostoderenda));
    }

    public static double calcularsalariodofuncionario (double numeroDeHorasTrabalhadas, double numeroDeHorasExtras) {

        double salarioinicial = numerodehorastrabalhadas * 18.6;
        double salariosdashorasextras = 5.7 * numerodehorasextras;

        double salariobruto = salarioinicial + salariosdashorasextras;
        return salariobruto;
        }

    public static double calcularImpostoDeRenda(double salariobruto){
        double impostoderenda = salariobruto * 0.49;
        return impostoderenda;
    }

    public static double calcularSeguridadeSocial (double salariobruto){
        double seguridadesocial = salariobruto * 0.94;
        return seguridadesocial;
    }

    public static double calcularSalarioLiquido (double salariobruto, double seguridadesocial, double impostoderenda){
    double salarioliquido = salariobruto - seguridadesocial - impostoderenda;
    return salarioliquido;
    }
}

How to correct?

  • 1

    Apparently it lacked contain AIDS in the println() use the + for that. Ex "ola "+ nome;

  • @rray worse than that, syntax error so.

  • Did any of the answers below solve your problem? Do you think you can accept one of them? Check out the [tour] how to do this, if you still don’t know how to do it. This helps the community by identifying the best solution for you. You can only accept one of them, but you can vote for any question or answer you find useful on the entire site (if you have enough score).

2 answers

6

The code is full of typos. Programming is detail. You need to organize the code, pay attention to what you are typing. You don’t just drop a text and get it done. You have to pay attention, be careful, be capricious. You have to read the mistakes, interpret them and correct them.

You can be mad at me, but I’ll tell you something that’s the most important thing right now and what’s gonna help you the most: you have to change the way you program, what you’re doing is just writing code, not creating programs. That way won’t get you anywhere.

Analyze character by character I changed. Yes, you will spend half an hour doing this, but you will learn something. If you think it’s a waste of time to do that, you’ll continue to struggle. Most are only case-sensitive, but also lack of concatenation of the text to be printed and lack of storage of function results in local variables.

You actually need to adopt a variable name pattern. Look to use camelCase in all variable names. Be consistent! I didn’t do it because it’s too much wrong, do it as an exercise. Be sure to do it! It may seem silly to organize like this, but over time you will realize that it is very important to facilitate reading and find errors more easily. All experienced programmers organize their code a lot. Start doing it now.

Nor will I mention the wrong use of double for monetary values, is not ready for this yet.

import java.util.Scanner;

class CalculoSalarioLiquido {
    public static void main(String args []) {
        System.out.println("Programa para calcular o Salário Líquido: ");
        System.out.println("Escreva o Número de Horas Trabalhadas pelo Funcionário: ");
        Scanner sc = new Scanner(System.in);
        double numerodehorastrabalhadas = sc.nextInt(); // <===== nome ruim
        System.out.println("Escreva o Número de Horas Extras Trabalhadas pelo Funcionário: ");
        double numerodehorasextras = sc.nextInt();
        double salariobruto = calcularsalariodofuncionario(numerodehorastrabalhadas, numerodehorasextras);
        double impostoderenda = calcularImpostoDeRenda(salariobruto);
        double seguridadesocial = calcularSeguridadeSocial(salariobruto);
        System.out.println("Salário Bruto: " + salariobruto);
        System.out.println("Imposto de Renda: " + impostoderenda);
        System.out.println("Seguro Social: " + seguridadesocial);
        System.out.println("Salário Líquido: " + calcularSalarioLiquido(salariobruto, seguridadesocial, impostoderenda));
    }

    public static double calcularsalariodofuncionario(double numeroDeHorasTrabalhadas, double numeroDeHorasExtras) { // Nomes bons
        double salarioinicial = numeroDeHorasTrabalhadas * 18.6;
        double salariosdashorasextras = 5.7 * numeroDeHorasExtras;
        double salariobruto = salarioinicial + salariosdashorasextras;
        return salariobruto;
    }

    public static double calcularImpostoDeRenda(double salariobruto) {
        double impostoderenda = salariobruto * 0.49;
        return impostoderenda;
    }

    public static double calcularSeguridadeSocial(double salariobruto) {
        double seguridadesocial = salariobruto * 0.94;
        return seguridadesocial;
    }

    public static double calcularSalarioLiquido(double salariobruto, double seguridadesocial, double impostoderenda) {
        double salarioliquido = salariobruto - seguridadesocial - impostoderenda;
        return salarioliquido;
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Guys, I thought, but you still don’t find the variables. Test in your IDE? I don’t know how to fix.

  • 3

    @Beginnerteemjava will not find it at all, have to write the minusculas and upper case. Vc ta mixing everything in your code. To tell you the truth, you’re trying to do some very complicated things for those who start you, I would suggest starting with the most basic. Learn the essentials first, or you’ll be like the eternal copiers and code collars that don’t learn to program.

  • I fixed all upper and lower case, but it’s still giving error in the last 3 System.out.println. You are not finding variables that have already been declared in the methods. Can help me?

  • 3

    For this you have to help yourself first. What error is giving? Great chance to have missed something yet. Declared the variables in the right scope? If you try to use a variable that is from another method, it will not work.

  • The errors are that the IDE cannot find in the last 3 System.out.println of the main method, the variables that have already been calculated in the later methods.

  • 2

    It doesn’t work like that. As I said in the previous comment, the variables do not go outside the scope in which they were declared. In main there is none of the variables even, the compiler does not miracle. It is the task of the programmer to save the return of the functions within the variables that will use.

  • All errors are syntax, you are not correctly concatenating the strings within the println. I recommend reading about it to avoid these problems http://www.tiexpert.net/programacao/java/string.php

  • 2

    @diegofm has scope error too, it is trying to use variables that do not exist, only pq used similar names in other functions. Unfortunately it started with a very complicated code pro current level of knowledge.

  • 2

    Actually there is an error that is not syntax. @Beginnerteemjava enjoy all comments and my new answer. Start with simpler things, you’re still missing basic details. Skipping a step will only get in the way. I understand you want to see cool things work, but it’s more fun to learn one step at a time.

  • Guys, I want to learn. Can you tell me exactly what I’m missing?

  • 2

    @Beginner Java read the full answer of the bigown, it is very enlightening. In small mistakes like this, sometimes it is better to break your head now alone while you are starting, than then in a big project, to be guilty of something that causes damage to a big company.

  • 3

    @All you have to do is re-read the comments and the many corrections that the bigown did, it’s pretty much all explained. Just that you have to read calmly and understand. Don’t worry, because getting your brains warmed up is part of learning. Read and reread as many times as you need, this will help not only in the code, but increase the ease of thinking. Take the time that the bigown invested in your question, and compare everything that has been changed, and try to understand the reasons.

  • Thank you very much, guys.

Show 8 more comments

4

In order to use a variable, it has to be in the scope of the method, in this case, the declaration of the variable salariobruto was missing. Java is a language that has "Case sensitive", IE, has difference between high and low-case letters. The parameters you are passing in System.out.println("Gross Salary: "(calcularsalariodofuncionario(numeroDeHorasTrabalhadas,numeroDeHorasExtras)); is different from what was declared, and to show String and values on the same line you have to use the + sign to concatenate the values. The names of your metedos that you are calling in System.out.println are also different from what was declared. Make these fixes that will work.

Browser other questions tagged

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