Advertisement
Guest User

traccia 1

a guest
Feb 17th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import java.util.Scanner;
  2. class Main {
  3.  
  4. public static void carica (int mat[][], int nr1, int nc1){
  5. Scanner leggi=new Scanner(System.in);
  6. int i, j;
  7. for(i=0; i<nr1; i++){
  8. for(j=0; j<nc1; j++){
  9. mat[i][j]=leggi.nextInt();
  10. }
  11. }
  12. }
  13.  
  14. public static boolean trova(int mat[][], int nr1, int nc1, int val1){
  15. boolean trovato = false;
  16. int i, j;
  17. for(i=0; i<nr1; i++){
  18. for(j=0; j<nc1; j++){
  19. if(val1==mat[i][j]){
  20. trovato=true;
  21. }
  22. }
  23. }
  24. return trovato;
  25. }
  26.  
  27. public static void main(String[] args) {
  28. Scanner leggi = new Scanner(System.in);
  29. int nr, nc, val;
  30. boolean trovato=false;
  31. do{
  32. System.out.println("Inserisci il numero di righe: ");
  33. nr=leggi.nextInt();
  34. }
  35. while(nr<=0);
  36. do{
  37. System.out.println("Inserisci il numero di colonne: ");
  38. nc=leggi.nextInt();
  39. }
  40. while(nc<=0);
  41. int mat1[][]=new int[nr][nc];
  42. System.out.println("Inserisci i valori della matrice: ");
  43. carica(mat1, nr, nc);
  44. System.out.println("Inserisci il valore da cercare: ");
  45. val=leggi.nextInt();
  46. trovato=trova(mat1, nr, nc, val);
  47. if(trovato==true){
  48. System.out.println("Il valore: " +val+ " è stato trovato.");
  49. }
  50. else{
  51. System.out.println("Il valore: "+val+" non è stato trovato.");
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement