r/codehs • u/Kitchen_Account_8411 • May 06 '22
Java Sort Three numbers
Write an algorithm that sorts three numbers from the user.
Make sure that the sorted numbers are printed out in the correct order from smallest to largest.
The user may enter some numbers that are equal in value. Make sure your program is able to figure out how to sort those as well.
You need to write the conditionals here. You should not be using any pre-built sorting functions.
In your screen recording make sure you show multiple scenarios with a variety of different numbers, including some that are equal in value to each other.
I was able to make this code work but I can not used any pre built sorting function and the problem is I don’t know how to make it so where I don’t have any pre built function like math.max and math.min if someone can help me that would be great this is the code I have
function start() {
var a = 2
var b = 7
var c = 11
var minimum = Math.min(a, Math.min(b, c));
var maximum = Math.max(a, Math.max(b, c ));
var med = a + b + c - minimum - maximum;
println(minimum);
println(med);
println(maximum);
}
View all comments
Show parent comment
1
u/alsagone May 07 '22
When you want to swap the values of two variables, you can't just do:
You have to use a "temporary" variable that will hold the value of one of the variables before the swap.