Created
June 23, 2022 15:32
-
-
Save racecraftr/d11f70b88f37a4c1e4caa11cc196fe91 to your computer and use it in GitHub Desktop.
Day 17 of the useless Java series.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package UselessJava.Day17; | |
import java.util.Scanner; | |
public class Day17 { | |
public String generateCheckerboard(int size){ | |
return generateCheckerboard(size, size); | |
} | |
public String generateCheckerboard(int width, int height){ | |
char[] chars = new char[]{'⬜','⬛'}; | |
String s = ""; | |
for(int i = 0; i < height; i++) { | |
for(int j = 0; j < width; j++) { | |
s += chars[(i + j) % chars.length]; | |
} | |
s += "\n"; | |
} | |
return s; | |
} | |
} | |
class Main{ | |
public static void main(String[] args) { | |
Day17 d = new Day17(); | |
Scanner sc = new Scanner(System.in); | |
while(true){ | |
System.out.println("Enter an integer."); | |
String s = sc.nextLine(); | |
if(s.matches("\\d+ *, *\\d+")){ | |
String[] strings = s.split(","); | |
int width = Integer.parseInt(strings[0].trim()); | |
int height = Integer.parseInt(strings[1].trim()); | |
System.out.println(d.generateCheckerboard(width, height)); | |
continue; | |
} | |
try{ | |
int size = Integer.parseInt(s); | |
System.out.println(d.generateCheckerboard(size)); | |
} catch(NumberFormatException e){ | |
System.out.println(Double.NaN); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment