vendredi 24 février 2017

how to incorporate a switch statement in this program

I am trying to incorporate a switch statement in the following code. My program is supposed to create a box with the letter X based on user input (ie. user enters 4 a 4x4 box is created. Extra credit would be adding a do while loop that asks the user if they want to draw another loop. Thank you in advance!

import java.util.Scanner;


public class Squares {

public static void main (String[] args) {

    int boxSize = 0;

    Scanner input = new Scanner(System.in);

    do {
        System.out.println("Please enter a positive number equal or less than 15.");
        boxSize = input.nextInt();

        if(boxSize == -1){
            System.exit(0);
        }

        if(boxSize <2 || boxSize > 15){
            System.out.println("Please try again.");
            continue;
        }

        for (int col = 0; col < boxSize; col++){
            for (int row = 0; row < boxSize; row++){
                if (row == boxSize - 1){
                    System.out.print("X");
                    if (row == boxSize -1) {
                        System.out.println();
                    }
                } else {
                    System.out.print("X");
                    if (row == boxSize - 1){
                        System.out.println();
                         }
                     }
                 }  
             }      
    }
        while(true);
    }

    }

Aucun commentaire:

Enregistrer un commentaire