0

You will take int two length of the side of a rectangle (n * m). You will then use a number of the character ‘O’ to print a rectangle to the screen.

The rectangle will be n ‘O’ in one side and m ‘O’ in other side. So id the user enters 4(col) and 3(row), the rectangle should look like:

OOOO

OOOO

OOOO

  1. Declare 2 variables: 1 for the number of row, and 1 for the number of col.

  2. Prompt the user to input the number of row and the number of col.

  3. Use loops (Hint: consider nest loops) to display the rectangle.

Note : You do not have to follow a particular input/output format, but I should be able to understand how to use and understand your program before looking at the code

Note: Don’t forget input validation! What’s a valid value for a row or a col?

Note: Does your program has the input limitation? (e.g. the range of input?)

What I Have so far:

public static void main(String[]args){

    int row;
    int column;

    Scanner input = new Scanner(System.in);


    System.out.print("Please enter an integer (row) greater than 0 and less than 15: ");
    row = input.nextInt();

    System.out.print("Please enter an integer (column) greater than 0 and less than 15: ");
    column = input.nextInt();

https://i.stack.imgur.com/C6NJx.jpg

Yatin
  • 2,969
  • 9
  • 34
  • 68
  • 2
    Your question is extremely simple, read: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html – Riley Carney Feb 13 '16 at 05:33
  • 1
    You've made a start, that's good. What is it that you are specifically confused about? There's plenty of documentation available about `for` loops, including nested for loops (which is a loop inside a loop). Perhaps you can explain why you are confused. – mhawke Feb 13 '16 at 06:26

1 Answers1

1

You should try solve yourself as part of the learning experience is struggling but if it might help you learn here is code with some comments in it.

        int row = 0;
        int column = 0;

        Scanner input = new Scanner(System.in);
        boolean nonValid = true; //if nonvalid is true the user input is wrong
        while (nonValid) { // keep asking user for input until it is according to your requirements
            System.out.print("Please enter an integer (row) greater than 0 and less than 15: ");
            row = input.nextInt();
            System.out.print("Please enter an integer (column) greater than 0 and less than 15: ");
            column = input.nextInt();
            if (row > 0 && row < 15 && column > 0 && column < 15) {//check user input here if it is good change nonValid to false to exit loop
                nonValid = false;
            } else {
                System.out.println("Bad Input try again!");
            }
        }
        for (int i = 0; i < row; i++) {//this loops rows
            for (int j = 0; j < column; j++) {//this loops columns
                System.out.print("O");//use print as you need each "O" next to each other in a row
            }//
            System.out.println("O");//after each row is printed use println(new line) to move to next line
        }
anaxin
  • 710
  • 2
  • 7
  • 16
  • Thanks anaxin, i did try to solve it my self but i guess i did not assign a value to row and column that is why i was not able to use if statement (it said that row did not have a value assigned). I will go through your code thoroughly and try to grasp it. PS: The code works, man i wish i could code like that, – adnansajid33 Feb 13 '16 at 15:07
  • Sorry to be a bother anaxin can you help me with the following code aswell? http://stackoverflow.com/questions/35519804/java-lottery-game-array/35519838?noredirect=1#comment58741154_35519838 – adnansajid33 Feb 21 '16 at 00:06