Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

How to display the numbers of this exercise in ascending order in java?

I got this exercise where i need to build a program in java that creates a 1-dimensional table where 10 integers will be stored, which will be read from the keyboard. At the end the program will display all the integers that are larger than average. (As you can see i've done this). But i need to display the numbers that are larger than average in ascending order. So there should be another instruction in the end, please help ?? I should say that im a beginner in java though. shuma=sum, mesatarja=average tabela = array though

please see it and help me solve this :)

Scanner in = new Scanner (System.in);

    int [] tabela = new int [10];
    
    System.out.print("Ju lutem jepni 10 nr te plote: ");
    
    for (int i = 0; i<tabela.length; i++) {
        tabela[i] = in.nextInt();
    }
    
    System.out.println("Tabela = " + Arrays.toString(tabela));

    int shuma = 0;
    
    for (int i = 0; i < tabela.length; i++)
    shuma = shuma + tabela[i];
    
    double mesatarja = shuma*1.0/tabela.length;
    
    System.out.println("Mesatarja e numrave eshte: " + mesatarja);
    
    System.out.print("Numrat me te medhenj se mesatarja jane: ");
    
    for (int i = 0; i < tabela.length; i++) {
        if (tabela[i] > mesatarja) {
        System.out.print(tabela[i] + ", ");
        }
        }
question from:https://stackoverflow.com/questions/65891999/how-to-display-the-numbers-of-this-exercise-in-ascending-order-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The simplest idea that comes to mind is to sort the array. You should use any sort algorithm after filling your array. Example using "buble sort":

for (int i = 0; i < tabela.length-1; i++) 
    for (int j = 0; j < tabela.length-i-1; j++) 
        if (tabela[j] > tabela[j+1]) 
           { 
             int temp = tabela[j]; 
             tabela[j] = tabela[j+1]; 
             tabela[j+1] = temp; 
           } 

Here you can find an explanation of the algorithms and their examples.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...