2 Replies - 1447 Views - Last Post: 02 October 2012 - 04:24 PM Rate Topic: -----

#1 MatrimC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-October 12

program in Java that sorts from least to greatest

Posted 02 October 2012 - 03:14 PM

I have created a program for my Computer Science class that the user inputs 3 integers and the program is supposed to sort them from least to greatest. I have written the code and it works fine, except when you put 2 integers as the same number. If I have put my numbers as 1, 1, and 4, the system prints The numbers from least to greatest are: 1, 0, 4. Here is my code, any help would be appreciated.

import java.util.Scanner;
	
public class Sorting{
	public static void main(String[] args){
	
	
	Scanner input = new Scanner(System.in);
	int low=0;
	int mid=0;
	int hi=0;
	
	System.out.println("Enter 3 variables:");
	int num1 = input.nextInt();
	int num2 = input.nextInt();
	int num3 = input.nextInt();
		
	//Finds the lowest number
	if ((num1 <= num2) && (num1 <= num3)) {
		low = num1;
	} else if ((num2 <= num1) && (num2 <= num3)) {
		low = num2;
	} else if ((num3 <= num1) && (num3 <= num2)) {
		low = num3;
		}
		
	//Finds the highest number
	if ((num1 >= num2) && (num1 >= num3)) {
		hi = num1;
	} else if ((num2 >= num1) && (num2 >= num3)) {
		hi = num2;
	} else if ((num3 >= num1) && (num3 >= num2)) {
		hi = num3;
		}
		
	//Finds the middle number
	if ((num1 != low) && (num1 != hi)) {
		mid = num1;
	} else if ((num2 != low) && (num2 != hi)) {
		mid = num2;
	} else if ((num3 != low) && (num3 != hi)) {
		mid = num3;
		}
		
	System.out.println("The variables from least to greatest are:");
	System.out.print("" + low);
	System.out.print("," + mid);
	System.out.print("," + hi);
	}
		}


Is This A Good Question/Topic? 0
  • +

Replies To: program in Java that sorts from least to greatest

#2 ThrowsException  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 83
  • Joined: 21-February 12

Re: program in Java that sorts from least to greatest

Posted 02 October 2012 - 04:08 PM

Your logic for finding the middle number is where it goes wrong. at that point you have set low = 1. So this happens

import java.util.Scanner;
	
public class Sorting{

	//low = 1, hi= 4
	
        //Finds the middle number
	if ((num1 != low) && (num1 != hi)) { //num1 == low mid does not get set
		mid = num1;
	} else if ((num2 != low) && (num2 != hi)) { //num2 == low mid does not get set
		mid = num2;
	} else if ((num3 != low) && (num3 != hi)) { //num3 == hi mid does not get set
		mid = num3;
		}



Through all this the value of mid never changes. I think you want to change your logic to see if it is between hi and low instead of not equal to.
Was This Post Helpful? 0
  • +
  • -

#3 UNIcoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 30-September 12

Re: program in Java that sorts from least to greatest

Posted 02 October 2012 - 04:24 PM

Hello there MatrimC.

You dont take in consideration that if num1 = 1 and num2 = 1, they are both low and mid value.
You have to write specific what value should be low / mid.
Something like this:

if ((num1 = num2) && (num3 >= num2)) {
    low = num1;
    mid = num2;
    hi = num3;
}


And the other two ways around:
if ((num3 = num2) && (num1>= num3)) {
       low = num2;
       mid = num3;
       hi = num1;
}else if ((num3 = num1 && (num2 >= num1)) {
       low = num3;
       mid = num1;
       hi = num2;
}


Define these three if-statements before the other onces and u should succed!
And look forward to use loops (for loops) its way easier :-)!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1