I can't find what's wrong with Java. Help me.

Asked 2 years ago, Updated 2 years ago, 21 views

public class Duplicate{

  public static void main(String[] args){
    double dishes = 0.0;
    int studentdiscount = 50;
    int workerdiscount = 25;
    int guestdiscount = 0;
    int discountreal = 0;
    double discountprice = 0.0;
    int isxmas = 0;

    dishes = Terminal.askFloat("how much is the dishes? ");
    isxmas = Terminal.askInt("is today xmas? (y: 1, n: 0)");

    discountreal = isxmas * studentdiscount; //instance1
    discountprice = dishes * (1 - discountreal/100.0); //instance1
    System.out.printf("\nit costs: %.2f dollar for student.\n", discountprice);

    discountreal = isxmas * workerdiscount; //instance2
    discountprice = dishes * (1 - discountreal/100.0); //instance2
    System.out.printf("\nit costs: %.2f dollar for workers.\n", discountprice);

    discountreal = isxmas * guestdiscount; //instance3
    discountprice = dishes * (1 - discountreal/100.0); //instance3
    System.out.printf("\nit costs: %.2f it costs: %.2f dollar for guests.\n", discountprice);

  }
}

The problem is that the repeated instance parts of the above code should be simplified using the method. The bottom part is what I did, but when I do it, I keep getting zero... I don't know if the variable declaration location is wrong or not. Please help me.ㅠ<

public class Test{

  public static double discountprice(double dishes, int xmas, int studentdiscount){
    return dishes * (1 - (xmas * studentdiscount/100.0));
  }

  public static void main(String[] args){
    double dishes = 0.0;
    int studentdiscount = 50;
    int discountreal = 0;
    double discountprice = 0.0;
    int isxmas = 0;

  dishes = Terminal.askFloat("how much is the dishes? ");
  isxmas = Terminal.askInt("is today xmas? (y: 1, n: 0)");

  System.out.printf("\nit costs: %.2f dollar for student.\n", discountprice);
  System.out.printf("\nit costs: %.2f dollar for workers.\n", discountprice*1.5);
  System.out.printf("\nit costs: %.2f it costs: %.2f dollar for guests.\n", discountprice*2);
  }
}

java

2022-09-21 18:53

1 Answers

We should call the method below.

...
...
System.out.printf("\nit costs: %.2f dollar for student.\n", discountprice(dishes, isxmas, studentdiscount));
System.out.printf("\nit costs: %.2f dollar for workers.\n", discountprice(dishes, isxmas, studentdiscount)*1.5);
System.out.printf("\nit costs: %.2f dollar for guests.\n", discountprice(dishes, isxmas, studentdiscount)*2.0);


2022-09-21 18:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.