DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   how to sort the output in descending (http://www.daniweb.com/forums/thread160182.html)

beginner21 Dec 1st, 2008 1:21 am
how to sort the output in descending
 
alright..
i have a problem on how to sort the output
my code is here:
public class Num7
{
        public static void main(String[]args)
        {
        char[] ch;
        ch=new char[20];
        int i;       
        for(i=0;i<ch.length;i++)
        System.out.print(i);
        }
}
the output is simply:

012345678910111213141516171819
all i want to do is to print first 19,18 and so on.
what should i modify?

verruckt24 Dec 1st, 2008 2:25 am
Re: how to sort the output in descending
 
I feel you are posting your class assignments here one by one, while we do not have any problem outright with that there should be some effort from your side that comes across from your query/ problem. We haven't seen any such effort as yet from you on this post.
Read this before posting such queries here.

beginner21 Dec 1st, 2008 3:17 am
Re: how to sort the output in descending
 
ok,
this is my modify code:

public class Num7
{
        public static void main(String[]args)
        {
       
        char[] ch={'1','2','3','4','5','6','7','8','9'};
        int x,y,z;       
        for(x=ch.length;x>0;x--)
        {
        System.out.print(x);
        }
        System.out.println();
        for(y=ch.length;y>0;y--)
        {
        System.out.print(y);
        }
        z=x*y;
        System.out.println("\nThe sum is: "+z);
        }
}
it display the values i entered on the array backward,
and that's what i want,
the problem is the value of z,
z is always 0;
i cant figure out whats the problem,
can you help?

quuba Dec 1st, 2008 3:50 pm
Re: how to sort the output in descending
 
In Your code
int x,y;
are indexes of loop - for.
When loops finished x=0 and y=0.Multipy them result is 0.
You not using
char[] ch={'1','2','3','4','5','6','7','8','9'};
table. More proper is declare table as int.
Analyse this:
    public static void main(String[] args) {

        int[] table = {9,8,7,6,5,4,3,2,1,0};
     
        for (int x = table.length-1; x >= 0; x--) {
            System.out.print("x=");
            System.out.print(x);
            System.out.print(" ");
            System.out.print("table[x]=");
            System.out.print(table[x]);
            System.out.println();
        }
        System.out.println();
    }

quuba


All times are GMT -4. The time now is 7:47 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC