Advertisement
Guest User

Untitled

a guest
May 6th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. import static java.lang.System.*;
  2. import java.util.*;
  3. import java.io.*;
  4. public class Sort5Integers {
  5. public static void main(String[] args) {
  6. int myloop[] = new int[5];
  7. Scanner input = new Scanner(System.in );
  8.  
  9. out.println("Enter 5 numbers.");
  10.  
  11. for (int y = 0; y < myloop.length; y++) //Inputs numbers into myloop array.
  12. {
  13. myloop[y] = input.nextInt(); //Why do you think the loop does not care if the numbers are inputted separately or in one line?
  14. }
  15. out.println("Numbers entered into array.");
  16.  
  17. int changed; //Why do you think having this is necessary?
  18. for (int sortloop = 0; sortloop < myloop.length; sortloop++) {
  19. for (int x = 0; x < myloop.length; x++) {
  20. if (myloop[sortloop] > myloop[x]) {
  21. changed = myloop[x];
  22. myloop[x] = myloop[sortloop];
  23. myloop[sortloop] = changed; //What do you think has happened here?
  24. }
  25. }
  26. }
  27. out.println("Sorting your numbers in descending order...");
  28.  
  29. //This For Loop Prints sorted numbers.
  30. for (int sortloop = 0; sortloop < myloop.length; sortloop++) {
  31. out.println(myloop[sortloop]);
  32. }
  33.  
  34. }
  35.  
  36. for (int sortloop = 0; sortloop < myloop.length; sortloop++) {
  37. for (int x = 0; x < myloop.length; x++) {
  38. if (myloop[x] > myloop[sortloop]) {
  39. changed = myloop[sortloop];
  40. myloop[sortloop] = myloop[x];
  41. myloop[x] = changed;
  42. }
  43. }
  44. }
  45.  
  46. for (int sortloop = 0; sortloop < myloop.length; sortloop++) {
  47. System.out.print(myloop[sortloop]);
  48. char asciiOfNumber = (char) myloop[sortloop];
  49. System.out.println(" " + asciiOfNumber);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement