0
import java.util.Scanner;
import java.text.DecimalFormat;
public class Temperature {
    int temp;
    static int counttemp =0;
    static int tempsum =0;
    //sentinel
    final static int SENTINEL =-1;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Temperature Program");
        Scanner scan = new Scanner(System.in);
        System.out.println("This program finds the sum of all temperatures " + " the temperatures");
        System.out.println("Enter a -1 to stop the program");

        System.out.println("Enter the first temperature: ");
        int temp = scan.nextInt();

        while (temp != SENTINEL);
        {
            //processing
            tempsum =+temp;
            counttemp ++;
            //updated read
            System.out.print("Enter the next temperature: ");
            temp = scan.nextInt();
        }
        System.out.println();
        if(counttemp !=0)
        {
            DecimalFormat oneDecimalPlace = new DecimalFormat("0.0");
            System.out.println("\nThe sum of all temperatures is " +
                  oneDecimalPlace.format((int)(tempsum) / counttemp));
        }
        else
            System.out.println("\nNo temperatures were entered");
        }               
    }
}

My code's purpose is for the user to enter temperatures then it gives the user a sum of all those temperatures entered. It stops running after I enter the first user input temperature. How can I get it to run smoothly? Any help would be appreciated thank you

CryptoFool
  • 17,917
  • 4
  • 23
  • 40

0 Answers0