-3

I will only take 3 values from the user, so there are only 3 possible answers. You can also assume that the user won't enter 0 three times and that it wouldn't matter if they entered the same value three times. I can create my own stuff for those scenarios (and I already have).

import java.util.Scanner;
import java.io.*;
import java.util.*;

public class Triangles
{
    public static void main (String[]args) throws IOException
        { 
        String a = "You have an";
        String b = "triangle";

        double l;
        Scanner lengthinput = new Scanner(System.in);

        double h;
        Scanner heightinput = new Scanner(System.in);

        double w;
        Scanner widthinput = new Scanner(System.in);

        System.out.println("Enter the length.");
        l = lengthinput.nextDouble();

        System.out.println("Enter the height.");
        h = heightinput.nextDouble();

        System.out.println("Enter the width.");
        w = widthinput.nextDouble();

        double max = l;

        if (w > max) max = w;
        if (h > max) {max = h;

The reason I'm struggling is because of the following:

I know the max, now I should just replicate the max code to create a minimum (since that's all I've learned just yet (intro CS class)). However, how would I know how to exclude the minimum value from being the one that I randomly select? Then the same for the medium value.

Tastybot
  • 55
  • 9
  • 2
    I was with you until the last two sentences. "Exclude the minimum value" etc.? "Randomly select"? I don't see any random selection going on. Please clarify. – Erick G. Hagstrom Dec 03 '15 at 14:10
  • So I basically do double max = l, to just start off my comparisons. But that is 'randomly' selected, since I have no idea what the actual max is. – Tastybot Dec 03 '15 at 14:12
  • 1
    To calculate the maximum, set the initial value to Integer.MIN_VALUE. To calculate the minimum, set the initial value to Integer.MAX_VALUE. That should solve that problem. – Fabian Damken Dec 03 '15 at 14:13
  • 1
    This problem is equivalent to sorting the three numbers, and then you just have the list `min, med, max`. – Alnitak Dec 03 '15 at 14:15
  • Looks like a homework question to me, but all i would do is record which number is the max and which number is the minimum after every entry by comparing the entry with the current holder of the title. User enters 1, max and min are 1, user enters 2, max is now 2, min is still 1. user enters 3, max is now 3, min is still 1. Print max and min, get 3 and 1. Though, the bubble sort method khelwood uses is better for only a few entries, and is kind of what you do already – Ryan Dec 03 '15 at 18:32

2 Answers2

6

You can find the min, medium and max of three values like this:

double a = ...;
double b = ...;
double c = ...;

if (a > b) {
  double swap = a;
  a = b;
  b = swap;
}

if (b > c) {
  double swap = b;
  b = c;
  c = swap;
}

if (a > b) {
  double swap = a;
  a = b;
  b = swap;
}

System.out.println("Min: "+a);
System.out.println("Med: "+b);
System.out.println("Max: "+c);
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    For other readers - this is just a trivial hard-coded bubble sort over three items – Alnitak Dec 03 '15 at 14:17
  • @Alnitak And I vowed never to write a bubblesort! It is probably not worth trying to hard code a merge sort on three values. – khelwood Dec 03 '15 at 14:20
  • +1 For other readers, @Alntiak is correct. This algorithim sorts a b c from the smallest to the largest. If a is bigger, it flips a and b. If b is currently bigger, b and c get flipped. Lastly, if whatever is in a is bigger, then a and b are flipped. – Kayla Dec 04 '15 at 00:28
-1

You can try something like below:

    float l = 2.0f;
    float h = 3.0f;
    float w = 1.5f;
    float[] arr = new float[] {l, h, w};
    Arrays.sort(arr);
    System.out.println("Min: " + arr[0] + ", Medium:  " +  arr[1]  + ", Max:  " +  arr[2]);
Wand Maker
  • 18,476
  • 8
  • 53
  • 87
  • It looks promising but I have no idea how to control arrays yet, especially for use in the rest of my code. It would be complicated for me but I think someone else may find it useful. – Tastybot Dec 03 '15 at 14:18
  • 1
    in any event, you are presumably being instructed about algorithms in your CS class, so just throwing the data into `Array.sort` is hardly going to teach you that. – Alnitak Dec 03 '15 at 14:19
  • 1
    @Alnitak No where in the problem statement it is mentioned that it should not use sort. If one uses if-else structure, the solution may work for 3 values, but code will become more messy as number of float arguments increases. In non-sort approach, just extending the solution to 4 floats itself scares me :-) – Wand Maker Dec 03 '15 at 14:24