my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Simple program: NEED HELP!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple program: NEED HELP!!" Watch "Simple program: NEED HELP!!" New topic
Author

Simple program: NEED HELP!!

Rohit Gupta
Greenhorn

Joined: Sep 12, 2000
Posts: 2
Hi,
I have been learning java for a couple of months...
I have been trying to write a simple program that will print 6 numbers, in ascending order, on the console. I have these numbers in an array:
int num[] = {46, 5, 17, 89, 77, 76}
how do I do this?...
Any help will be appreciated.
Thanks
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
Do you want to print the numbers in the array, in ascending order, no matter how they are placed in the array? This would be so you can play with logic flow.
Or do you just want to print those numbers in ascending order?
Rohit Gupta
Greenhorn

Joined: Sep 12, 2000
Posts: 2
Yep, I want to use the logic flow...can you help me with some code?
Thanks.
Eric Edwards
Ranch Hand

Joined: Feb 12, 2000
Posts: 60
Originally posted by Steven YaegerII:
Do you want to print the numbers in the array, in ascending order, no matter how they are placed in the array? This would be so you can play with logic flow.
Or do you just want to print those numbers in ascending order?

Try this!
import java.util.*;
public class NewSortArray
{
public static void main(String[] args)
{
int[] num = {46,5,17,89,77,76};
Arrays.sort(num);
for (int i = 5; i < num.length; --i)
System.out.println(num[i]);
}
}
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi!
just try this code:
class example
{
public static void main(String args[])
{
int[] num = {46,5,17,89,77,76};
int temp = 0 ;
for(int i=0;i<num.length;i++)>
{
for(int j=0;j<num.length;j++)>
{
if(num[i] < num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
for(int j=0;j<num.length;j++)>
{
System.out.println(num[j]);
}
}
}

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi!
just try this code:
class example
{
public static void main(String args[])
{
int[] num = {46,5,17,89,77,76};
int temp = 0 ;
for(int i=0;i<num.length;i++)>
{
for(int j=0;j<num.length;j++)>
{
if(num[i] < num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
for(int j=0;j<num.length;j++)>
{
System.out.println(num[j]);
}
}
}

 
Don't get me started about those stupid light bulbs.
 
subject: Simple program: NEED HELP!!