#Having trouble with this coding exercise
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
your scanner should not be static
you should not decalre multiple variables in a comma-separated list
and the problem asks to read in the number of containers of each size, you only have a single variable for it
recommended starter code: ```java
public class variableExercises5 {
public static final int CENTS_PER_SMALL_CONTAINER = 10;
public static final int CENTS_PER_LARGE_CONTAINER = 25;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
}
}
you should have static constants defined like I have to prevent having 'magic numbers' in your code that you don't know what they correlate to.
Also note I used int and not double for money because double's are not precise. For this its probably fine but note that 0.1 + 0.2 != 0.3 due to repeating binary decimals
hence you could end up being off by a cent or two if you dont use integers to store the base unit (cents)
just divide by 100 at the end to be lazy, or parse the string of the integer representation
but dividing by 100 and then applying %0.2f in a string format will suffice
or something similar to that
import java.util.Scanner;
public class ContainerRefundCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of containers (1 liter or less): ");
int smallContainers = scanner.nextInt();
System.out.print("Enter the number of containers (more than 1 liter): ");
int largeContainers = scanner.nextInt();
double smallContainerRefund = smallContainers * 0.10;
double largeContainerRefund = largeContainers * 0.25;
double totalRefund = smallContainerRefund + largeContainerRefund;
System.out.printf("The total refund for returning the containers is: $%.2f%n", totalRefund);
}
}
If a variable you need to output needs decimals, it will always be a double/float. If a variable you need to output does not need decimals, you will use int
Looks like this code does it really well. The only thing to keep in mind with it would be changing %nto\n
former is platform independent, latter isn't
it's generally the correct way do so it
yes bc in platforms like windows, \n is the incorrect way of doing newline
a newline in windows is \r\n
Huh... I did not know that
Closed the thread due to inactivity.
If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍
Closed the thread due to inactivity.
If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍