Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple program: NEED HELP!!

 
Rohit Gupta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, I want to use the logic flow...can you help me with some code?
Thanks.
 
Eric Edwards
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]);
}
}
}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic