Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. package parcial;
  2.  
  3. import javax.swing.JOptionPane;
  4. import javax.swing.JTextArea;
  5.  
  6. public class Parcial {
  7.  
  8. public static void main(String[] args) {
  9. int matriz[][], fila, columna, filas, columnas, opcion;
  10. String salida;
  11. JTextArea hoja = new JTextArea();
  12.  
  13. do {
  14. filas = Integer.parseInt(JOptionPane.showInputDialog("Ingrese las filas"));
  15. } while (filas < 0 || filas > 20);
  16. do {
  17. columnas = Integer.parseInt(JOptionPane.showInputDialog("Ingrese las columnas"));
  18. } while (columnas < 0 || columnas > 20);
  19. matriz = new int[filas][columnas];
  20.  
  21. for (fila = 0; fila < filas; fila++) {
  22. for (columna = 0; columna < columnas; columna++) {
  23. int dato;
  24. do {
  25. dato = Integer.parseInt(JOptionPane.showInputDialog("Ingrese el dato"));
  26. } while (dato < 0 || dato > 400);
  27. matriz[fila][columna] = dato;
  28. }
  29. }
  30.  
  31. do {
  32. opcion = Integer.parseInt(JOptionPane.showInputDialog("Que desea hacer?\n1. Mostrar matrz.\n2. Cambiar numero.\n3. Ordenar ascendentemente.\n4. Ordenar desendentemente."));
  33. if (opcion == 1) {
  34. salida = "";
  35. for (fila = 0; fila < filas; fila++) {
  36. for (columna = 0; columna < columnas; columna++) {
  37. if (columna == columnas - 1) {
  38. salida += matriz[fila][columna];
  39. } else {
  40. salida += matriz[fila][columna] + "\t";
  41. }
  42. }
  43. if (fila == fila - 1) {
  44.  
  45. } else {
  46. salida += "\n";
  47. }
  48. }
  49. hoja.setText(salida);
  50. JOptionPane.showMessageDialog(hoja, hoja);
  51. } else if (opcion == 2) {
  52. int buscar, nuevo;
  53. nuevo = -1;
  54. do {
  55. buscar = Integer.parseInt(JOptionPane.showInputDialog("Ingrese el numero que desea cambiar"));
  56. } while (buscar < 0 || buscar > 400);
  57. for (fila = 0; fila < filas; fila++) {
  58. for (columna = 0; columna < columnas; columna++) {
  59. if (matriz[fila][columna] == buscar) {
  60. if (nuevo == -1) {
  61. do {
  62. nuevo = Integer.parseInt(JOptionPane.showInputDialog("Ingrese el numero"));
  63. } while (nuevo < 0 || nuevo > 400);
  64. }
  65. matriz[fila][columna] = nuevo;
  66. }
  67. }
  68. }
  69. if (nuevo == -1) {
  70. JOptionPane.showMessageDialog(null, "Numero no econtrado");
  71. }
  72. } else if (opcion == 3) {
  73. int cambio,burbuja;
  74. for (int i = 0; i < (filas*columnas); i++) {
  75. for (fila = 0; fila < filas; fila++) {
  76. for (columna = 0; columna < columnas; columna++) {
  77. if (columna<columnas-1) {
  78. if (matriz[fila][columna+1]<matriz[fila][columna]) {
  79. burbuja=matriz[fila][columna+1];
  80. matriz[fila][columna+1]=matriz[fila][columna];
  81. matriz[fila][columna]=burbuja;
  82. }
  83. }else if(columna==columnas-1 && fila!=filas-1){
  84. cambio=matriz[fila+1][0];
  85. if(cambio<matriz[fila][columna]){
  86. burbuja=cambio;
  87. cambio=matriz[fila][columna];
  88. matriz[fila][columna]=burbuja;
  89. matriz[fila+1][0]=cambio;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. } else if (opcion == 4) {
  96. int cambio,burbuja;
  97. for (int i = 0; i < (filas*columnas); i++) {
  98. for (fila = 0; fila < filas; fila++) {
  99. for (columna = 0; columna < columnas; columna++) {
  100. if (columna<columnas-1) {
  101. if (matriz[fila][columna+1]>matriz[fila][columna]) {
  102. burbuja=matriz[fila][columna+1];
  103. matriz[fila][columna+1]=matriz[fila][columna];
  104. matriz[fila][columna]=burbuja;
  105. }
  106. }else if(columna==columnas-1 && fila!=filas-1){
  107. cambio=matriz[fila+1][0];
  108. if(cambio>matriz[fila][columna]){
  109. burbuja=cambio;
  110. cambio=matriz[fila][columna];
  111. matriz[fila][columna]=burbuja;
  112. matriz[fila+1][0]=cambio;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119.  
  120. } while (opcion > 0 && opcion <= 4);
  121.  
  122. }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement