Advertisement
merlinaccio

Untitled

Jan 25th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.        
  9.         // 29.1-ArrayBattagliaNavale
  10.        
  11.        
  12.        
  13.         String nave = "X";
  14.         int cont = 0;
  15.         int c, r;       // c colonna, r riga
  16.        
  17.         String matr [][] = new String [5][5];
  18.        
  19.         int cColpo = inputInt("Inserisci la colonna");
  20.        
  21.         do {
  22.             if (cColpo >= 0 && cColpo <5) {
  23.                 int rColpo = inputInt("Inserisci riga");
  24.                 if (rColpo >= 0 && rColpo <5) {
  25.                     for (int i=0; i<5; i++) {
  26.                         c = (int) (Math.random()*5);
  27.                         r = (int) (Math.random()*5);
  28.                         System.out.print(c + " " + r + " ");
  29.                         matr[c][r] = nave;
  30.                         if (c == cColpo && r == rColpo) {
  31.                             cont += 1;
  32.                             System.out.println("\tColpite " + cont);
  33.                         } else {
  34.                             System.out.println("\tAcqua");
  35.                         }      
  36.                     }
  37.                 }   else {
  38.                     System.out.println("Riga inesistente, riprova con numeri compresi tra 0 e 4");
  39.                 }
  40.             } else {
  41.                 System.out.println("Colonna inesistente, riprova con numeri compresi tra 0 e 4");
  42.             }
  43.         } while (cont < 5);
  44.     }
  45.  
  46.     // metodi
  47.    
  48.     static int inputInt(String messaggio) {
  49.         String s = JOptionPane.showInputDialog(messaggio);
  50.         return Integer.parseInt(s);
  51.     }
  52.        
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement