0

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?

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by quuba
0

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.

0

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?

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.