-1

I'm new to coding. I'm practicing by coding a program that gives an actor his/her pay after subtracting his/her agent's cut. Why does it output an incorrect calculation? I can't seem to figure out the problem.

I searched on the web and I'm convinced that my math is right. There should be no reason for it to be outputting anything other than the correct answer.

Let's say the actor's pay is $100.00, and the agent's cut is 15%. The actor should get %85.00, right? Strangely, the console outputs $600.

I know this goes against the site's rules, but I find it's important to copy and paste the entire project, but it's not that long. Here it is:

.

.

import java.util.Scanner;

public class Main {

static int payBefore;
static int percentageTaken;
static int totalAfterCut;
static int percentageDividedBy100 = percentageTaken/100;
static int middleOfCalculation;

static Scanner payBeforeSc = new Scanner(System.in);
static Scanner percentageSc = new Scanner(System.in);
static Scanner middleOfCalculationSc = new Scanner(System.in);

public static void main(String args[]) {

    incomeAndPercentageQuestions();

}

private static void incomeAndPercentageQuestions() {

    System.out.println("How much did your job pay before any agent cuts?");
    payBefore=payBeforeSc.nextInt();

    System.out.println("What percentage is your agent taking?");
    percentageTaken=percentageSc.nextInt();

    calculation();

}

private static void calculation() {

    totalAfterCut = payBefore * (100 / percentageTaken);

    output();

}

private static void output() {

    System.out.println("$" + totalAfterCut + " will be your pay after your agent's cut.");

}

}

  • 15% is 15 / 100, not 100 / 15. You're using `(100 / percentageTaken)`. And, when using integer division, 100 / 15 is 6. – JB Nizet Aug 15 '19 at 20:18

1 Answers1

2

I think it has to do with your math being incorrect. You have the wrong order for the division. It should be like this:

totalAfterCut = payBefore * (percentageTaken / 100.0); //Actually amount deducted

And as Nexevis pointed out, your math returns the amount deducted not the amount after the deduction. So also change it so the amount deducted is subtracted from payBefore:

totalAfterCut = payBefore - (payBefore * (percentageTaken / 100.0));

You should also be using floating-point datatypes when dealing with these kinds of values because with integers, the decimal part will be truncated off. So that means that 15/100 (both integers) will always result in 0. You can leave percentageTaken as an integer and then use 100.0 (instead of 100) to get a floating point value. And also make sure you are saving the result into a floating-point datatype if you are concerned about the cents portion of the value.

double totalAfterCut;
totalAfterCut = payBefore - payBefore * (percentageTaken / 100.0);

I'd also change payBefore to a floating-point datatype so the user can enter in the exact amount including cents.

double payBefore;
payBefore=payBeforeSc.nextDouble();
  • Should be `totalAfterCut = (payBefore - payBefore * (percentageTaken / 100.0));`, he wants the amount HE has afterward, not the cut. So he wants the 85 printed not the 15. – Nexevis Aug 15 '19 at 20:29
  • 2
    Right you are! I was so caught up on other things I completely overlooked the logic part of what the OP was trying to do here. Thanks for the comment! – Daniel Gilroy Aug 15 '19 at 20:35