0

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:

Scanner scan = new Scanner(System.in);
int temp = scan.nextInt();

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

Welbog
  • 59,154
  • 9
  • 110
  • 123
  • To start, do you understand what the question is asking? Or are you more having trouble converting your plan into code? What do you think the sentinel should be? – Welbog Oct 30 '20 at 13:18
  • I am having trouble converting the plan into code and I think that the sentinel value should be a print statement value with user input. – Audrey Washington-Wright Oct 30 '20 at 13:21
  • A sentinel value usually refers to a variable that controls the loop's execution - e.g. a counter or a boolean that tells the loop when to stop. Based on the problem statement, it seems like it should be something that indicates that the user is done providing inputs. – Welbog Oct 30 '20 at 13:24
  • So how do I write it? – Audrey Washington-Wright Oct 30 '20 at 13:27
  • 1
    The problem is leaving that up to you. What do you think it should be? Common examples I've seen include having the user specify the number of inputs they want to provide, or prompting for more inputs until the user enters a specific keyword (e.g., "quit"). – Welbog Oct 30 '20 at 13:29
  • Can you please just show me how to do it? I'm really stressed and this is due today. – Audrey Washington-Wright Oct 30 '20 at 13:31
  • 2
    As a general rule, Stack Overflow is not a homework-writing service. We can provide you with pointers and tips, not outright answers. – Welbog Oct 30 '20 at 13:32
  • adding to the hints @Welbog has given ... ```scan.hasNext()``` will tell if the user has provided more inputs or not. u can keep it inside the while condition. – user728627 Oct 30 '20 at 13:36
  • @AudreyWashington-Wright - Check https://stackoverflow.com/questions/64518114/how-do-you-set-up-an-infinite-loop-in-scanners/64518701#64518701 for an example. You can find hundreds of such examples on SO. – Arvind Kumar Avinash Oct 30 '20 at 13:40
  • None of this helps. I don't know what to do and I really need an answer. – Audrey Washington-Wright Oct 30 '20 at 13:42
  • If I were you I will do a looping --- ask the user to input the temperature, put them into array (or just the total sum and the count will also be sufficient) and calculate the average, then display the average (in double precision floating value) until the user enters a negative number . The reason is that the temperature will never be a negative value – Ken Lee Oct 30 '20 at 13:58

2 Answers2

0

The sentinel is any non-numeric string:

public class Sentinel {

    public static void main(String args []) {
        Scanner scan = new Scanner(System.in);
        String temp = scan.next();
        int count = 0;
        double total = 0;
        while (isNumeric(temp))
        {
            total += Double.parseDouble(temp);
            count++;
            temp = scan.next();
        }
        System.out.println(BigDecimal.valueOf(total/count).setScale(3, RoundingMode.HALF_UP).doubleValue());
    }

    public static boolean isNumeric(String str) {
        return str.matches("-?\\d+(\\.\\d+)?");  //match a number with optional '-' and decimal.
    }
}

Your original code is also trying to convert from fahrenheit to celsius even though the problem statement doesn't say anything about the scale.

0
int n = scan.nextInt();           //to input number of days in a given month
int[] temp = new int[n];          //an array to store the low temps of all days
int i=0, total=0;
double avg=0.0;
while (i<n)
{   
    temp[i] = scan.nextInt();     //input temperature per day one at a time
    total=total+temp[i];          //to get the sum total of all days of the month
    i++                           //increment the counter i
}
avg=total/n;
System.out.println("the average low temperature is :"+avg);```