#Having trouble with this coding exercise

1 messages · Page 1 of 1 (latest)

radiant gulchBOT
#

<@&987246399047479336> please have a look, thanks.

radiant gulchBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

hard knot
#

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)

hard knot
#

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

past raven
#
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);
    }
}
fervent atlas
#

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

fervent atlas
frail egret
#

it's generally the correct way do so it

fervent atlas
#

Really? A way to break a line is %n?

#

I thought that was the intention

frail egret
#

yes bc in platforms like windows, \n is the incorrect way of doing newline

#

a newline in windows is \r\n

fervent atlas
#

Seriously??

#

Huh, the more you know

frail egret
#

%n picks the correct one for the current platform

#

by using System.lineSeparator()

fervent atlas
#

Huh... I did not know that

radiant gulchBOT
#

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 👍

radiant gulchBOT
#

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 👍