Java

Well I don’t understand this code can it be broken down in simple format? And also I want to print it out and format it using printf instead of DF.format. Any help will be highly appreciated. Thankyou

int numberOfAutomobilesCanShip = 0;

numberOfAutomobilesCanShip = MaxNumberOfAutomobiles * NumberOfRailcarsAvailable > numberOfAutomobilesToShip ? numberOfAutomobilesToShip : MaxNumberOfAutomobiles * NumberOfRailcarsAvailable;

System.out.println(“Number of automobiles that can be shipped:\t” + numberOfAutomobilesCanShip + ", " + DF.format(PercentOfAutomobileCanShip));

you could write that code using an if statement:

if (MaxNumberOfAutomobiles * NumberOfRailcarsAvailable > numberOfAutomobilesToShip) {
  numberOfAutomobilesCanShip = numberOfAutomobilesCanShip;
}
else {
  numberOfAutomobilesCanShip = MaxNumberOfAutomobiles * NumberOfRailcarsAvailable;
}
System.out.printf("Number of automobiles that can be shipped:\t %d, %f", numberOfAutomobilesCanShip, PercentOfAutomobileCanShip);  
//assumes numberOfAutomobilesCanShip is a double
ternary operator

a = b > c ? e : f;
is the same as writing

if (b > c) {
  a = e;
}
else {
  a = f;
}

link to your identical post: Need some help on thus Java Code