Java array reading loop never ends? -


hi new programming , today writing code 1 java array task , in beginning tried test have done , in first loop (the array reading ) program not stop read numbers enter number (n) length. please ?

import java.util.scanner;   public class readtwoelementsforarrayandsum {      public static void main(string[] args) {         // todo auto-generated method stub          scanner in = new scanner(system.in);          system.out.println("please enter n element:");         int n = in.nextint();          system.out.print("please enter k element, k < n: ");         int k = in.nextint();          int[] arrayn = new int[n];         system.out.print("please enter n numbers array:  ");          for(int = 0; < arrayn.length; i++) {             arrayn[i] = in.nextint();         }         boolean changed = false;         {             int temp = 0;                        for( int = 0; < (arrayn.length-1); i++) {                 if(arrayn[i] > arrayn[i+1]){                     temp = arrayn[i];                     arrayn[i] = arrayn[i+1];                     arrayn[i+1] = temp;                     changed = true;                  }             }          } while (changed);          for(int = 0; < arrayn.length; ++) {             system.out.printf("%d", arrayn[i]);             system.out.print(k);         }           in.close();       }  } 

loop infinite because once if condition inside loop executed changed set true , value never changed false causing infinite loop while loop.

instead can use arrays.sort(arrayn) or if want use loop try below code

 int count = 0;  {     int temp = 0;     count++;     (int = 0; < (arrayn.length - 1); i++) {         if (arrayn[i] > arrayn[i + 1]) {             temp = arrayn[i];             arrayn[i] = arrayn[i + 1];             arrayn[i + 1] = temp;           }     }  } while (count < (arrayn.length)); 

demo


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -