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);
}
}
2 Replies - 392 Views - Last Post: 02 October 2012 - 04:24 PM
#1
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.
Replies To: program in Java that sorts from least to greatest
#2
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
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.
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.
#3
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:
And the other two ways around:
Define these three if-statements before the other onces and u should succed!
And look forward to use loops (for loops) its way easier :-)!
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 :-)!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|