Question

In: Computer Science

How can I do the star pattern looks like: ( for example) ( you might not...

How can I do the star pattern looks like: ( for example)

( you might not notice the difference but there is space at the beginning of row 2 & 4)

.* * * * *

. * * * * *

.* * * * *

. * * * * *

.* * * * *

instead of :

. * * * * *
. * * * * *
. * * * * *
. * * * * *
. * * * * *

This is the program that I'm trying to do :

import java.util.Scanner;

public class checkerBoard {
  
  
  
public static void main(String args[]) {
  
  
Scanner scan = new Scanner(System.in);

int number;
  
System.out.print("enter one integer value: ");
number = scan.nextInt();

  
for (int i = 0; i < number; i++)
{
for (int j = 0; j < number; j++)
{
System.out.print(" *");

}
System.out.println();
  
}
}
}

Solutions

Expert Solution

In case of any query do comment. Thanks

I have changed your code, in such cases you have to see the row number which you are trying to print have odd value at index then print extra space in outer loop for i.

Code:

import java.util.Scanner;

public class checkerBoard  {
  
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int number;
        System.out.print("enter one integer value: ");
        number = scan.nextInt();
        for (int i = 0; i < number; i++)
        {
            if (i % 2 ==1)
                //print a space for each odd row (used odd becuase index starts from 0 for first row )
                System.out.print(" "); 
            for (int j = 0; j < number; j++)
            {
                System.out.print(" *");
            
            }
            System.out.println();
        }
    }
}

========Screen shot of the code=======

output:


Related Solutions

How do I calculate the up-market Beta? Can you give me an example please? Can you...
How do I calculate the up-market Beta? Can you give me an example please? Can you give an examp;le of when it is was a good market timer and when it is a bad market timer
I would like you to think and write about how you might define integrity in the...
I would like you to think and write about how you might define integrity in the dimension of character leadership for yourself (500 words)
How can I make a problem like this simpler? and what equation do I use for...
How can I make a problem like this simpler? and what equation do I use for part D? Thanks! A researcher wonders if mental simulation will help one more successfully achieve one’s goals. To test this idea, the researcher recruited six students from a general psychology class, several weeks before their first midterm. After their first midterm, the researcher got the students’ scores on this test from their professor. Several weeks later, each of the students was brought into the...
Can you provide an example on how a decision tree might be used in practice?
Can you provide an example on how a decision tree might be used in practice?
Give an example of a real-world system that looks nothing like a mass on a spring...
Give an example of a real-world system that looks nothing like a mass on a spring but still behaves like a harmonic oscillator. The weirder the better! can you please explain the example as well
I want this exercise with an example of a bullet, do the best you can make...
I want this exercise with an example of a bullet, do the best you can make please and take your time, this depends on saving the semester. Pose a problem situation of real life, where you need mechanical physics to solve it. The problem posed must contain the following topics, without being limited to this list: 1. Conservation of Mechanical Energy. 2. Conservation of the linear or two-dimensional moment. 3. Conservation of the angular Moment.
Every time I attempt to execute this, I get an inputmismatchexception, which looks like this: Oops!...
Every time I attempt to execute this, I get an inputmismatchexception, which looks like this: Oops! Read Exception: java.util.InputMismatchExceptionInitial input: Input sorted by grade Input sorted selection This is my code. import java.util.*;   import java.io.*; import java.io.PrintStream; public class jack { PrintStream prt = System.out; int n; int i; int j; double grade[]; String name[]; jack() { try { Scanner inf = new Scanner(System.in); n=inf.nextInt(); inf.nextLine(); grade=new double[n]; name = new String[n]; for (int i=0; i<n; i++){ name[i] = inf.next();...
Can someone explain how to manipulate the clock in verilog code? For example, I do not...
Can someone explain how to manipulate the clock in verilog code? For example, I do not understand how to go from 100MHz to 68027Hz.
Please find an example illustrating how IPR system can contribute monopoly like Microsoft I mentioned.
Please find an example illustrating how IPR system can contribute monopoly like Microsoft I mentioned.
Writing python code: Can you do for me this. The question looks bellow Overview : As...
Writing python code: Can you do for me this. The question looks bellow Overview : As a programmer, you have be asked by a good friend to create a program that will allow them to keep track of their personal expenses. They want to be able to enter their expenses for the month, store them in a file, and then have the ability to retrieve them later. However, your friend only wants certain people to have the ability to view...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT