Im stuck on how to display 5 integers in ascending order.i only have the idea to input 3 integers.what is my lacking to my code.Can you figure it out.thank you so much.hope you gonna help me

import java.util.*;
public class ascendingorder
{
static Scanner console = new Scanner(System.in);

public static void main(String[]args)
{

int x,y,z;

System.out.print("Enter the first number: " );
x = console.nextInt();
System.out.println();

System.out.print("Enter the second number: " );
y = console.nextInt();
System.out.println();

System.out.print("Enter the third number: " );
z = console.nextInt();
System.out.println();




if ((x<y) && (x<z))
{
System.out.print("Numbers in ascending order are: " +x );

if (y<z)
System.out.println(" "+ y + " " + z );

else
System.out.println(" " + z + " "+ y );
}

else

if ((y<x) && (y<z))

{

System.out.print("Numbers in ascending order are: " +y );

if (x<z)
System.out.println(" "+ x + " " + z );

else
System.out.println(" " + z + " "+ x );
}

else

if ((z<x) && (z<y))

{

System.out.print("Numbers in ascending order are: " +z );

if (x<y)
System.out.println(" "+ x + " " + y );

else
System.out.println(" " + y + " "+ x );

}





}


}

Recommended Answers

All 7 Replies

Can you show/post the input to and the output from your program?

Have you studied arrays yet? What your code needs is an array to hold the values in as they are being read in.
Once they are read in you can sort them and then print them.

Can you show/post the input to and the output from your program?

Have you studied arrays yet? What your code needs is an array to hold the values in as they are being read in.
Once they are read in you can sort them and then print them.

no i not study arrays yet.my mentor said.i need to create a program a long method without using the methods or package in arrays like array.sort.

the output looks this way
enter first number: 9(example input by the user)
enter 2nd number:5 (example input by the user)
enter 3rd number: 2 (example input by the user)
numbers in ascending order are 2 5 9

BUT my mentor said I need to input 5 numbers or integers...thats only my problem.im stuck or i get confused what to do next.what should i add to my program sir.

It will be a terrible way to write your program without using arrays. I can't imagine why your mentor says you can't use them.
If you really can't use an array, you'll have to create enough variables to hold all the input values and .... I can't go on. This is NOT the way to write this code.
Maybe someone else will come along with a suggestion.

Good luck.

one of my classmate shows like this but i dont know how they do it..i only need to input 5 integers and output is 5 integers are in ascending order...pls help me

no i not study arrays yet.my mentor said.i need to create a program a long method without using the methods or package in arrays like array.sort.

BUT my mentor said I need to input 5 numbers or integers...thats only my problem.im stuck or i get confused what to do next.what should i add to my program sir.

Your mentor never said not to use arrays, He said that you need to input 5 numbers.
Also what he meant was not to use any java methods that do the sorting for you. Yes there is method where you enter numbers and it sorts them. But your mentor said not to use that method (array.sort)

You'd better ask him if you can use arrays:

int [] numbers = new int[5];

// or better

int N = 5;
int [] numbers = new int[N];

Also in your code you are reading 3 numbers. Reading 5 wouldn't be a problem. Just call the method you are using 5 times instead of 3. And you have 3 variables. How difficult it is to figure out to use 5 variables instead.

BUT it is NOT a good idea to use that approach with 5 numbers. The ifs that you will have to use are approximately 5! = 5x4x3x2x1 = 120 (I think), if you choose to expand the code you have.

For more than 3 numbers you need arrays and sorting algorithm.

So you'd better check with your teacher if you can use arrays.
Also are you sure that you need to display all the numbers in ascending order?

yes she said to use Array but not the package like array.sort
she want manual build up
can u help me.or can you give me a code
that input 5 integers and display it to ascending order
thank you

java masters

Since you need 5 numbers, create an array of ints with length 5. Then have a for loop. Inside it call the console.nextInt() in order to get each number and put it in the array. With each loop you will fill each element of the array. The index of the loop would be the index of the array. Arrays of length 5 go from 0 to 4.

I am sure your teacher has given you notes about arrays and how to use for loops. Then use any sorting algorithm you have been taught or search at the net and come back with some code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.