Display three non-negative integers in increasing order
Prompt the user to type in three non-negative integers. Display the integers in an increasing order.
import java.util.Scanner;
{
int number1,number2,number3;
Scanner keyboard = new Scanner (System.in);
number1=keyboard.nextInt();
number2=keyboard.nextInt();
number3=keyboard.nextInt();
if(number1<number2&&number1<number3&&number2<number3)
{
System.out.println(number1);
System.out.println(number2);
System.out.println(numbe开发者_高级运维r3);
}
else if(number1>number2&&number1>number3&&number2>number3)
{
System.out.println(number3);
System.out.println(number2);
System.out.println(number1);
}
else if(number1<number2&&number1<number3&&number2>number3)
{
System.out.println(number1);
System.out.println(number3);
System.out.println(number2);
}
else if(number2>number1&&number2>number3&&number1>number3)
{
System.out.println(number3);
System.out.println(number1);
System.out.println(number2);
}
else if (number3>number1&&number3>2&&number1>number2)
{
System.out.println(number2);
System.out.println(number1);
System.out.println(number3);
}
Output:
2 1 3
1 2 3
P.S. I am not allowed to use while statement, swing and for. I am newbie at java so this what I could come up with. Is there a faster way? Thank you.
This is an implementation of bubble sort. It is not much better than your implementation but gives a glimpse into how you would use a loop in the future. What happens is that after the first 2 blocks, the largest value will be in the third spot. The next block then fixes the remaining two numbers. You could move the if clauses into a separate function that accepts as parameters the two numbers you want to compare and switch.
public static void main(String[] args) {
int number1,number2,number3;
Scanner keyboard = new Scanner (System.in);
number1=keyboard.nextInt();
number2=keyboard.nextInt();
number3=keyboard.nextInt();
int temp;
if (number2 < number1){
temp = number2;
number2 = number1;
number1 = temp;
}
if (number3 < number2){
temp = number3;
number3 = number2;
number2 = temp;
}
if (number2 < number1){
temp = number2;
number2 = number1;
number1 = temp;
}
System.out.println(number1);
System.out.println(number2);
System.out.println(number3);
}
boolean n2 ;
if(num1>num2) {
if(num1>num2 && num1>num3){
if(num2>num3){
System.out.println(num1+">"+num2+">"+num3);
n2=true;
}
else {
System.out.println(num1+">"+num3+">"+num2);
}
}
else if(n2){
if(num1<num3){
System.out.println(num2+">"+num3+">"+num1);
}
}
}
else {
System.out.println(num2+">"+num1+">"+num3);
}
Speed is not an issue. Correctness is. What happens if number1 and number3 are equal? That said, here is a more compact piece of logic that should be fairly efficient.
if (number2 < number1)
{
temp_number = number1;
number1 = number2;
number2 = temp_number;
}
if (number3 < number2)
{
temp_number = number2;
number2 = number3;
number3 = temp_number;
if (number2 < number1)
{
temp_number = number1;
number1 = number2;
number2 = temp_number;
}
}
System.out.println(number1);
System.out.println(number2);
System.out.println(number3);
Your way is fast enough for three numbers but what happens if the next assignment requires you to do the same task on a list of 10 integers? Or 100? Or 1,000,000?
As you continue learning to program you will learn that the performance of the strategy you choose to solve a problem (the algorithm) can vary immensely depending on the size of the input. The strategy you chose in your attempt of this problem was to enumerate all the ways that three numbers can relate by size - not too bad since there are only six - but for even ten numbers there are millions of ways! A better choice of algorithm would be to store them in a structure where their size increases from item to item (by sorting them) since most sorting algorithms will still perform well with very large input sets.
Since it is homework, i think a nested if with it's else's would be a cleaner solution. It works also correctly with equal values, negative values and does not swap the values of the variables. :-)
int number1, number2, number3;
Scanner keyboard = new Scanner(System.in);
number1 = keyboard.nextInt();
number2 = keyboard.nextInt();
number3 = keyboard.nextInt();
if (number1 < number2) {
if (number2 < number3) {
System.out.println("123 " + number1 + number2 + number3);
} else {
if (number1 < number3)
System.out.println("132 " + number1 + number3 + number2);
else
System.out.println("231 " + number3 + number1 + number2);
}
} else {
if (number3 < number2) {
System.out.println("321 " + number3 + number2 + number1);
} else {
if (number3 < number1)
System.out.println("312 " + number2 + number3 + number1);
else
System.out.println("213 " + number2 + number1 + number3);
}
}
精彩评论