Method Error in Java

It can be simple or quite difficult ... I have no idea about any way. Here is my code:

package looping;

import java.util.Scanner;

public class PayrollMethods2 {

    public static void main(String[] args) {

        int hours = 0;
        double wr = 0;
        double pay;

        getData(hours, wr);

        pay = calculatePay(hours, wr);

        System.out.println(pay);
        // TODO Auto-generated method stub

    }


    public static void getData(Integer hours, Double wr)
    {
        Scanner kb = new Scanner(System.in);


        System.out.print("Please enter your wage rate ");
        wr = kb.nextDouble();

        System.out.print("Please enter your hours work ");
        hours = kb.nextInt();
    }



    public static double calculatePay(int hours, double wr)
    {
        if(hours <= 40)
            return hours * wr;
        else
            return (40 * wr) + ((hours - 40) * (1.5* wr));
    }

}

I want to return the "getData ()" method so that when I enter the hours and wr on the keyboard, it returns the total. However, it returns 0.0 no matter what I click. I know that this is due to the fact that I am assigned the value 0 to variables, and also that the method is "invalid". But I do not know how to change the getData () method so that it returns the correct values.

+4
source share
4 answers

, , , hours wr getData(), getData wr.

main: (initialize)

Variable    Value    Memory Address
hours         0       44444
wr            0.0     55555

getData: wr ,

Variable    Value    Memory Address
hours         0       66666
wr            0.0     77777

: - 10, wr - 20

Variable    Value    Memory Address
hours         10       66666
wr            20.0     77777

main: wr stil 0

Variable    Value    Memory Address
hours         0       44444
wr            0.0     55555

:

( Java):

class Salary
{
  int hours;
  double wr;
}

getData Salary Pay

public static Salary getData()
{
    Scanner kb = new Scanner(System.in);
    Salary sal = new Salary();

    System.out.print("Please enter your wage rate ");
    sal.wr = kb.nextDouble();

    System.out.print("Please enter your hours work ");
    sal.hours = kb.nextInt();

    return sal;
}

public static double calculatePay(Salary sal)
{
}
+3

wr, , getData , wr .

. , , . ( , , ).

+2

Java . , , , main.

Java , , getData main.

+2

, , . Integer -? .

psv main (String[] args) {
    Integer i = new Integer(1);
    increment(i);
    System.out.println(i); // Prints 1!
}

void increment(Integer i) { i = i + 1; }

2, 1. , Integer .

, , , . , , . , !

Integer Integer, increment.

-1

Source: https://habr.com/ru/post/1528832/


All Articles