Search code examples
javasquare

Having problems with overlapping inner square within a larger square in Java


I am a total beginner in java and programming in general. Trying to make a career switch and learn Java. Been doing loops and trying different tasks.

I want to make a square within a larger square, and I'm having a problem. Here's my code:

import java.util.Scanner;

public class SquareLoop {

private static Scanner input;

public static void main(String[] args) {

    input = new Scanner(System.in);
    System.out.println("Please enter your star value: ");
    int value = input.nextInt();
    System.out.println("Please enter the second square value: ");
    int value2 = input.nextInt();

    for (int i = 0; i <= value; i++) {
        for (int j = 0; j <= value; j++) {
            if(i != value2 || i != value && j != value2 || j != value)
                System.out.print(" ");
            if (i == 0 || i == value  || i == value - value2 || i == value2)
                System.out.print("*");
            else if (j == 0 || j == value || j == value - value2 || j == value2)
                System.out.print("*");

            else
                System.out.print(" ");
        }
        System.out.println();
    }
}
}

Here's my result My result

Any help appreciated. I don't want a solution, but guidelines a bit more specific maybe.


Solution

  • I think your trouble is with the relational and logical operators you're using. You'll get a brief overview here: http://www.tutorialspoint.com/java/java_basic_operators.htm. Or for a more detailed look at general principles of logical operators: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Operators/Logical_Operators

    But as a generally, I've placed below some useful general information, a clarification of what your code is doing now, and some suggestions at the bottom.

    OR Operator - ||

    TRUE  || TRUE  = TRUE
    TRUE  || FALSE = TRUE
    FALSE || TRUE  = TRUE
    FALSE || FALSE = FALSE
    

    AND Operator - &&

    TRUE  && TRUE  = TRUE
    TRUE  && FALSE = FALSE
    FALSE && TRUE  = FALSE
    FALSE && FALSE = FALSE
    

    Relational Operators

    == //Specific Equality (A == B = TRUE iff A has the same value as B)
    <, > //Less Than / Greater Than (A < B or A > B -- excludes B)
    <=, >= //Less Than / Greater Than *or Equal To* (A <= B or A >= B -- inclusive of B)
    != //Specific Inequality
    

    Given the above, from your question I take it you have this

    and you want something like this

    Current Code:

    If that's about the gist of it, I recommend taking another look at the logic you're using. What I'll do here is clarify the code you've got and hopefully you can take this information to troubleshoot accordingly:

    First let's look at your for-loops:

    for (int i = 0; i <= value; i++) {
        for (int j = 0; j <= value; j++) {
    

    Currently, you're using a for-loop that goes from 0 to the inputted value, inclusive. This means that the stars printed actually print one more star than the inputted number (0, 1, 2 .. value-1, value)

    Next your If-statements:

    if(i != value2 || i != value && j != value2 || j != value)
        System.out.print(" ");
    

    This if statement actually ends up always being true because of order of operations (replace " " with "x" and see the result). I don't think the if statement is necessary here, but you can use the following resource to get the gist of operators precedence/order of operations: http://introcs.cs.princeton.edu/java/11precedence/ . Mind you, to change the order of operations in an if-statement is that same as in algebra -- using parenthesis. i.e.

    (Order of Operations: Left to Right)
    False || True && True && True
        => True && True && True
        => True && True 
        => True
    
    
    (False || True) && (True && False)
        => (True) && (True && False)
        => (True) && (False)
        => False
    

    Your second if-statement:

    if (i == 0 || i == value  || i == value - value2 || i == value2)
        System.out.print("*");
    

    The above code prints a star for the entire column where i = 0, value, value-value2, or value2. Let's take the example of value = 7, value2 = 3

    Currently (and remember i goes from 0 to 7):

    i == 0 grabs:
    // * - - - - - - -
    // * - - - - - - -
    // * - - - - - - -
    // * - - - - - - -
    // * - - - - - - -
    // * - - - - - - -
    // * - - - - - - -
    // * - - - - - - -
    
    i == value grabs:
    // - - - - - - - *
    // - - - - - - - *
    // - - - - - - - *
    // - - - - - - - *
    // - - - - - - - *
    // - - - - - - - *
    // - - - - - - - *
    // - - - - - - - *
    
    i == value - value2 (7 - 3 = 4) grabs:
    // - - - - * - - -
    // - - - - * - - -
    // - - - - * - - -
    // - - - - * - - -
    // - - - - * - - -
    // - - - - * - - -
    // - - - - * - - -
    // - - - - * - - -
    
    i == value2 (3) grabs:
    // - - - * - - - -
    // - - - * - - - -
    // - - - * - - - -
    // - - - * - - - -
    // - - - * - - - -
    // - - - * - - - -
    // - - - * - - - -
    // - - - * - - - -
    

    Similarly, a look at your third if statement:

    else if (j == 0 || j == value || j == value - value2 || j == value2)
        System.out.print("*");
    

    Currently:

    j == 0 grabs:
    // * * * * * * * *
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    
    j == value grabs:
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // * * * * * * * *
    
    j == value - value2 (7 - 3 = 4) grabs:
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // * * * * * * * *
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    
    and j == value2 (3) grabs:
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // * * * * * * * *
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    // - - - - - - - -
    

    And lastly:

    else
        System.out.print(" ");
    

    As expected, if your code doesn't put down a "*", this else prints a " ".

    Some Recommendations, Thoughts, & Resources