Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.util.Scanner;
public class Descending_Order
{
    public static void main(String[] args) 
    {
   Scanner sc = new Scanner(System.in);
        int  n = sc.nextInt();
        int a[] = new int[n];
        for (int i = 0; i < n; i++) 
        {
            a[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) 
        {
            for (int j = i + 1; j < n; j++) 
            {
                if (a[i] < a[j]) 
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        for (int i = 0; i < a.length; i++) 
        {
          a[i]=sc.nextInt();
       }
            System.out.print(a[i] + " ");
        }
     }
}


What I have tried:

error:cannot find symbol
System.out.print(a[i]+" ");
^
Symbol:variable i
Location:class main
I am getting like this.
Posted
Updated 23-Dec-20 2:37am
v2

If you fixed your indentation it would be obvious that you have closed your for loop too early:
Java
 for (int i = 0; i < a.length; i++)
 {
   a[i]=sc.nextInt();
}          // <----- this ends the scope of variable i remove this brace
     System.out.print(a[i] + " ");  // the variable i does not exist here.
 }
 
Share this answer
 
You haven't declared temp at all, you have two different declarations of i in the same scope:
Java
        for (int i = 0; i < n; i++) 
            {
            for (int j = i + 1; j < n; j++) 
                {
...
                }
            for (int i = 0; i < a.length; i++) 
                {
...
                }
...
            }
You bubble sort some values, then overwrite them all again ...

That looks like it was thrown together without any significant thinking about what you expected to produce.
Stop and think about what you are doing before you start coding: it will save you a lot of time in the long run! This may help you: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900