简体   繁体   中英

How do I use another method in a method?

I want to use the method preisProKilo() in the method gesamtpreis() ? Its about calculating the total price of a product. preis pro kilo = price per kilo and gesamtpreis = total amount .

class Kaffeeladen {

  double preisProKilo(double grundpreis) {        
        return (grundpreis + 2.19) * 1.19;
  }

  double gesamtpreis(int gewuenschteMenge, double preisProKilo) {      
    return preisProKilo * gewuenschteMenge; 
    //Here its not using the method "preisProKilo" from above. 
  }

  public static void main(String[] args) {
    Kaffeeladen k = new Kaffeeladen();

    double preis1 = k.preisProKilo(3.00);
    System.out.println("Bei einem Grundpreis von 3,00 Euro kostet 1 kg Kaffee inklusive Steuern " + preis1 + " Euro.");


  }
}

You are getting confused because you named the method and variable the same thing. Don't do this.

You are not calling a method at all, you are only using the variable.

It should be something like this:

double preisProKilo(double grundpreis) {
    return (grundpreis + 2.19) * 1.19;
}

double gesamtpreis(int gewuenschteMenge, double newValueName) {
    return preisProKilo(newValueName) * gewuenschteMenge; 
}

You can change the name of newValueName to whatever you choose, just please try to use different names.

Additionally, I do not see anywhere you are actually using gesamtpreis(int, double) , so you would need to use this as well by doing in your main something like this:

Kaffeeladen k = new Kaffeeladen();
double value = k.gesamtpreis(2, 3.00);

You probably wanted to give gewuenschteMenge that is the desired quantity and grundpreis that is the basic price in your gesamtpreis or full price method.

class Kaffeeladen {

  double preisProKilo(double grundpreis) {        
        return (grundpreis + 2.19) * 1.19;
  }

  double gesamtpreis(int gewuenschteMenge, double grundpreis) {      
    return preisProKilo(grundpreis) * gewuenschteMenge; 
  }

  public static void main(String[] args) {
    Kaffeeladen k = new Kaffeeladen();

    double preis1 = k.preisProKilo(3.00);
    System.out.println("Bei einem Grundpreis von 3,00 Euro kostet 1 kg Kaffee inklusive Steuern " + preis1 + " Euro.");


  }
}

You are actually using preisProKilo variable over calling the method preisProKilo(double grundpreis) to do so you have to call it as shown in the code above.

I'm not sure exactly what you're trying to do here, as the naming of variables is the same as the method names, but to call a method, you need to use the syntax methodToCall(arg1, arg2) .

It seems you have a lack of understanding of how methods work. One way to think of it is in terms of math functions. For example,

f(x)= x + 1

If x is 1, f(x) has a value of 2. If x is 2, f(x) has a value of 3, and so on.

So the same way to write that in Java would be as follows:

public int addOne(int x) {
  return x + 1;
}

And to call that method, you'd do this.

public int doWork(int value) {
  // doing work...
  value = addOne(value);
  // doing more work...
  return value;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM