Can someone please show me how to do this code?

I am really struggling to understand how to write this while loop based on the instructions. Can someone please show me what to do? I already tried to write my own code below and it’s bad.

Here are the instructions:

Write a sentinel controlled while loop that will allow you to calculate the average low temperature for any month. The average temperature should be displayed as a properly calculated double value. Explain why you chose the sentinel value. The code for the input of the initial temperature value is provided.

Here is my code:

int temp = scan.nextInt();

while ()
{
    temp = 5.0/9.0 * (temp - 32.0);
    System.out.println()
}

You need a condition to stop code execution. The point of the assignment is to choose a value that will stop program execution and why you chose that one.

2 Likes

I tried this code but it’s not working the way I need it to when I run it in repl.it.

Scanner scan = new Scanner(System.in);
int temp = scan.nextInt();
int i = 0;
int total = 0;
double avg = 0.0;
while (i < temp)
{
	temp =  scan.nextInt();
	total = total + temp;
	i++;
}
avg = total/temp;
System.out.println(“The average low temperature is:” + avg);

How are you expecting it to work? What is it doing differently.

Also, you’re not following the instructions. You’re not using a sentinel value.

I don’t know and I am really stressed out. This assignment is due today and I don’t know what to do and I just need someone to show me how to code this assignment.

We are not going to do your homework for you. We are happy to help you as you are working through your assignment, but you need to do the work.

1 Like

Hey @csquare121 why are you changing the temp variable if you want to use it as a sentinel value ?

I don’t know what I’m doing anymore. I need a lot of help.

Some freeCodeCamp lessons on control statements might be helpful:



1 Like
Scanner scan = new Scanner(System.in);

double temp = 0;
double total = 0;
double avg = 0;
int count = 0;

while (temp >= 0)
{
	temp =  scan.nextDouble(); 
	total += temp;
	count++;
}

avg = total / count;
System.out.println(“The average low temperature is:” + avg);

Try this.
Not a Java dev but I have some basic knowledge.
Why less than zero for sentinel value.

Absolute zero is the lowest possible temperature where nothing could be colder and no heat energy remains in a substance. … By international agreement, absolute zero is defined as precisely; 0 K on the Kelvin scale, which is a thermodynamic (absolute) temperature scale; and –273.15 degrees Celsius on the Celsius scale.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

I have added [spoiler] and [/spoiler] tags on the line above and below your solution code.

2 Likes

Hello, I’m sorry she said that it is urgent thats why I posted the solution, but we are having a conversation right now. I did tell her next time to research way ahead of time of the assignment.

1 Like

When someone is helping and not giving a solution it’s for a reason.

1 Like