-5

I cant seem to figure it out. I need to ask the user for 3 floats, got it. Having trouble with the output of the high and low part. Any help would be awesome!

package temp;
import java.util.Scanner;
/**
 *
 * @author Pherman
 */
public class Temp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
         float temp1;
         float temp2;
         float temp3;
    float highTemp;
    float lowTemp;

    System.out.print("Please enter your first temperature");
        temp1=scan.nextFloat();
    System.out.print("Please enter your second temperature");
        temp2=scan.nextFloat();
    System.out.print("Please enter your third temperature");
        temp3=scan.nextFloat();

    }

 }
  • 3
    Where is your high-low logic?? – TheLostMind Jan 23 '14 at 14:25
  • Please describe your problems in detail! – Uli Köhler Jan 23 '14 at 14:27
  • Store the user-input values in a collection of some kind (array?), sort it, then display the first and last values in the collection. Ideally, wrap a class around that logic. The class would have a private collection of values and three functions. One which adds values to the collection, one which returns the highest value, one which returns the lowest value. The logic in the add-value function would maintain the sort of the private collection. – David Jan 23 '14 at 14:28
  • Sorry! I'm very new at this. I was hoping I could just say highTemp = ?, lowTemp = ? and output. The only thing left for me to do is somehow output low and high temp – user3228151 Jan 23 '14 at 14:31
  • If I NEED an array, thats where I'm stuck. Haven't done that yet – user3228151 Jan 23 '14 at 14:32
  • Thanks so much! I think the professor wanted a solution without arrays because we haven't touched on that yet. VERY informative, Thanks again! – user3228151 Jan 23 '14 at 14:43

4 Answers4

2

Here is a tip for you.

http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html

float highTemp = Math.max(temp1, Math.max(temp2, temp3); // high

float lowTemp = Math.min(temp1, Math.min(temp2, temp3); // low

float middleTemp = (temp1 + temp2 + temp3) - highTemp - lowTemp; // middle

In this approach, there is no need to use if, switch, array or to sort.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • I like the lack of arrays, but it won't scale as well as the other solutions (although I suppose if five were given, you could find the high-low of the middle three, then use that to find out the middle) – Chris Forrence Jan 23 '14 at 14:46
2

I would use an array, and Arrays.sort(float[]) like so

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    float[] temp = new float[3];

    for (int i = 0; i < temp.length; ) {
        System.out.printf("Please enter your temperature #%d ", i);
        if (scan.hasNextFloat()) {
            temp[i] = scan.nextFloat();
            i++;
        } else {
            scan.next();
        }
    }
    java.util.Arrays.sort(temp);
    System.out.println("Low = " + temp[0]);
    System.out.println("High = " + temp[temp.length - 1]);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

Using Collections to min/max

List<Float> myList = new ArrayList<Float>();

System.out.print("Please enter your first temperature");
myList.add(scan.nextFloat());
System.out.print("Please enter your second temperature");
myList.add(scan.nextFloat());
System.out.print("Please enter your third temperature");
myList.add(scan.nextFloat());

System.out.println(Collections.min(myList));
System.out.println(Collections.max(myList));

You can also use a loop to get your input instead of using System.out.print

Zzyrk
  • 907
  • 8
  • 16
0

Initialize highTemp and lowTemp whenever you can and update their values when necessary.

Use the if keyword.

StephaneM
  • 4,779
  • 1
  • 16
  • 33