Skip to content

Instantly share code, notes, and snippets.

@applegateaustin
Created October 3, 2012 23:47
Show Gist options
  • Save applegateaustin/3830616 to your computer and use it in GitHub Desktop.
Save applegateaustin/3830616 to your computer and use it in GitHub Desktop.
AsciiArt
import java.util.Scanner;
public class AsciiArt1 {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
//declaring variables
int userInput;
int space = 5;
int width = 0;
int num = 0;
int oddOrEven = -1;
//asking the user for input
System.out.println("Choose one of the following patterns by typing the corresponding number: ");
System.out.println("1) 5x5 Grid");
System.out.println("2) Checker Board");
System.out.println("3) Reverse Diagonal");
userInput = keyboard.nextInt();
if(userInput == 1){
//loop for 5x5 grid
for(int i = 0; i < space; i++){
System.out.print(num);
for(int j = 0; j < space; j++){
System.out.print(" *");
}
System.out.println();
num++;
}
}
else if(userInput == 2){
//loop to make Checker Board
for(int i = 0; i < space; i++){
System.out.print(num + " ");
num++;
for(int j = 0; j < space; j++){
if((i % 2 == 0 && j % 2 == 0) || (i % 2 == 1 && j % 2 == 1))
System.out.print(" *");
if( (i % 2 == 1 && j % 2 == 0) || (i % 2 == 0 && j % 2 == 1))
System.out.print(" ");
}
System.out.println();
}
}
else if(userInput == 3){
for (int i = 0; i < space; i++){
System.out.print(num + " ");
num++;
for(int j = 0; j < space; j++){
}
System.out.println();
}
//loop to make Reverse Diagonal
/*while (width < 5){
System.out.print(num + " ");
if(num == 4)
System.out.print("*");
if(num == 3)
System.out.print(" *");
if(num == 2)
System.out.print(" *");
if(num == 1)
System.out.print(" *");
if(num == 0)
System.out.print(" *");
width++;
num++;
System.out.println();
}*/
}
}
}
@timsegraves
Copy link

Hmm, having a hard time thinking of a clue without just doing it. Here's my first thought of how I'd do it:

// Reverse Diagonal
for (int i =0; i < space; i++) {
  System.out.print(num + " ");
  num++;

  for (int j=0; j < space; j++) {
    if (j + i == 5) {
      System.out.print(" *");
    else {
      System.out.print(" ");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment