I have a question about the triangular output and rhombus output using the Java scanner.

Asked 2 years ago, Updated 2 years ago, 80 views

// Triangular scanner

import java.util.Scanner;

public class Stars05 {
    public static void main(String[] args) {
        int line;
        Scanner input = new Scanner(System.in);

        System.out.print("How long is the line? : ");
        line = input.nextInt();

        for(int i=line, add=0; i>0; i--, add++) {
            for(int j=0; j<i-1; j++) {
                System.out.print(" ");
            }
            for(int j=0; j<line-(i-1)+add; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
// diamond scanner

import java.util.Scanner;

public class Stars07 {
    public static void main(String[] args) {
        int line;
        Scanner input = new Scanner(System.in);

        System.out.print("How long is the line?(only odd-number) : ");
        line = input.nextInt();

        for(int i=line/2, add=0; i>0; i--, add++) {
            for(int j=0; j<=i-1; j++) {
                System.out.print(" ");
            }
            for(int j=0; j<line/2 - (i-1)+add; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for(int i=0, add=line/2; i<line/2 + 1; i++, add--) {
            for(int j=0; j<i; j++) {
                System.out.print(" ");
            }
            for(int j=0; j<(line/2 + 1)-i+add; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

I wanted to make a triangle and diamond print out based on the values entered using the scanner and for statement I didn't know how to make it, so I tried to take the googled thing and interpret it, but I didn't know how to interpret it.

I would appreciate it if you could give me advice on interpreting this code and how to make what I want well. And I want to make it the same way using the while statement.

java scanner for while-loop

2022-09-20 21:39

1 Answers

Functions such as input/output can be familiar and found quickly as you use them, but it is better to think about the logic process yourself.

In a triangle problem, the key is how many characters each line should print out to create a triangle.

For example, if you want to create a three-line triangle when you turn the program around

2 spaces and 1 star in the first line

In the second row, there is 1 space and 3 stars

In the third row, there are 0 spaces and 5 stars

Exit Program

If you see a pattern, make the top 3 lines into n lines.

In the first line, there are n-1 spaces and 1 star

In the second row, there are n-2 spaces and 3 stars

In the third row, there are n-3 spaces and 5 stars

In the i-th line, there are n-i spaces, 2*i-1 stars,

loop (n) times {
    loop (n-1) times
        1 blank character output;
    loop (2*i-1) times
        1 star printout;
    Open character output;
}

Look for patterns, write pseudocodes, and then follow the code lines and think about how the values change and what is printed.

The code you brought to Google seems to be a little complicated because there are two initialization and increase and decrease expressions in the for statement at the beginning of learning. In addition, the equation for calculating the number of stars became difficult due to the intertwining of two variables.

If you change the part that outputs the star in the same way as the calculation formula I wrote, it is as follows.

for(int j=0; j<2*add-1; j++) {
    System.out.print("*");
}

To change the for statement to a while statement, initialize the variable in the previous step of the while statement, and write the conditional statement used for and the increase or decrease expression in the last line of the while statement. Below is an example.

/* for */
for(int i=line, add=0; i>0; i--, add++) {
    ...
}
/* /* while */
int i=line;
int add=0;
while(i>0) {
    ...
    i--;
    add++;
}

Using the for statement as above helps reduce the number of code lines and helps you focus on important areas.

In addition, since variables exist only inside the for statement (scope), you can prevent duplicate initialization of variables when used elsewhere.

(For example, if the j variable in the code above is not a for statement, you must reinitialize it to zero or use a different variable name.)

If it's not because of the assignment, we recommend the for statement.

Additionally, if you're just starting to learn, it'll be much more helpful to try using it yourself, establish logic, and write code than to take other codes and interpret them.

Properly written code is easy to read with annotations and names representative names, but simple functional code is more difficult to interpret than writing because each person has different intentions.

Thank you.


2022-09-20 21:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.