Advertisement
Garro

Java - Cuadrado de 0 y 1

Dec 11th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. /*Cuadrado de ceros y unos: En el programa principal pida dos enteros,
  2.  * H y L. Dibuje un rectángulo de n filas y m columnas formado por "0"
  3.  * y "1" alternados, tal que las filas pares comiencen con un 0 y las
  4.  * impares con un 1.*/
  5. import java.util.*;
  6. public class tarea08{  
  7.     public static void main (String args[]) {
  8.         Scanner tec = new Scanner(System.in);
  9.         System.out.print("Ingrese el numero de filas: ");
  10.         int H = tec.nextInt();
  11.         H = validar(H,"filas");
  12.         System.out.print("Ingrese el numero de columnas: ");
  13.         int L = tec.nextInt();
  14.         L = validar(L,"columnas");
  15.         cuadrado(H,L);     
  16.     }
  17. public static int validar(int V,String nombre){
  18.     Scanner tex = new Scanner(System.in);
  19.     while (V<=0){
  20.         System.out.println("Error, numero no valido!");
  21.         System.out.print("Ingrese el numero de "+nombre+": ");
  22.         V = tex.nextInt();
  23.     }return V; 
  24. }
  25. public static void cuadrado(int H, int L){
  26.     for(int h=0;h<H;h++){
  27.         for(int l=0;l<L;l++){
  28.             if((l+h)%2==0){
  29.                 System.out.print("1 ");
  30.             }else{
  31.                 System.out.print("0 ");
  32.             }}
  33.         System.out.println();}
  34. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement